0

I want to output only 1 name data from my array which is:

Array
(
    [results] => Array
      (
        [0] => Array
            (
                [0] => some name
                [1] => Founder
            )

        [1] => Array
            (
                [0] => some name
                [1] => Marshal
            )

        [2] => Array
            (
                [0] => some name
                [1] => Marshal
            )

        [3] => Array
            (
                [0] => some name
                [1] => Royal Knight
            )

        [4] => Array
            (
                [0] => some name
                [1] => Knight
            )
    )

)
1

now I use :

echo "<pre>";
echo print_r($API->getSearchFreeCompanyMembers());
echo "</pre>";

to print the array which seems to work fine but when I try selecting single array like:

echo "<pre>";
echo print_r($API->getSearchFreeCompanyMembers(1)[0]);
echo "</pre>";

all I get is 1 on the page

Any help would be much appreciated, if any more code needed please let me know.

6
  • How come you changed getSearchFreeCompanyMembers() to getSearchFreeCompanyMembers(1) ? Commented Oct 4, 2013 at 12:11
  • 1
    Can you explain what getSearchFreeCompanyMembers() does and how parameters affect output? Commented Oct 4, 2013 at 12:11
  • $API->getSearchFreeCompanyMembers[1][0] Commented Oct 4, 2013 at 12:12
  • You can accept one answer at a time but it's ok :-). Commented Oct 4, 2013 at 12:24
  • thank you all for the great answers been a great help Commented Oct 4, 2013 at 12:47

4 Answers 4

3

You seem to have changed the arguments when calling the method. getSearchFreeCompanyMembers

Your first example shows getSearchFreeCompanyMembers() The second is getSearchFreeCompanyMembers(1)

To get the first element in the array returned by the method.

1. Dereference as you did (just do not put 1 as argument).

$result = $API->getSearchFreeCompanyMembers()[0]

Beware the side effect is that the rest of the array returned is discarded. Also this feature is only available from <= 5.4

2. Save the returned array to a variable and pick the first element

$array = $API->getSearchFreeCompanyMembers();
print_r($array[0]);

For more info on arrays and for dereference see example #7 http://php.net/manual/en/language.types.array.php

Sign up to request clarification or add additional context in comments.

Comments

2

You may try something like this

$array = $API->getSearchFreeCompanyMembers();
echo $array['results'][0][1]; // first name (Founder)
echo $array['results'][1][1]; // second name (Marshal)

Or, use (for some name)

echo $array['results'][0][0]; // first ('some name')
echo $array['results'][1][0]; // second ('some name')

Here, results is an associative array and the first array is [0] and every array ([0], [1]) in results contains an array. So, it's something like this

$array = Array(
    'results' => Array (
        Array ( 'some name', 'Founder' ), // 1st array in results
        Array ( 'some name', 'Marshal' )  // 2nd array in results
    )
);

Comments

0
echo "<pre>";
$members = $API->getSearchFreeCompanyMembers();
print_r($members['results'][0]); //no echo needed
echo "</pre>";

1 Comment

Hi, this answer came up for review as low quality. While I appreciate it's been a few years since you posted this answer, would you mind expanding on this with an explanation to help other users that may read this answer in future?
0

like to print array results key [0] value [1] i.e founder. you have to call array like this
before print_r there is no need of echo

        echo "<pre>";
        print_r($results [0][0]);
        echo "</pre>";

similarly for

 echo "<pre>";
$employee=$API->getSearchFreeCompanyMembers();
print($employee['results'][1][0]);
echo "</pre>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.