0

I am fetch facebook user's working history, and when I print_r, I get an array like this:

Array (
    [work] => Array (
        [0] => Array (
            [employer] => Array (
                [id] => 111178415566505 
                [name] => Liputan 6 SCTV
            )
      ) [1] => Array (
            [employer] => Array (
                [id] => 107900732566334
                [name] => SCTV
            )
        )
    ) 
    [id] => 502163984
)

How do I display only the value from name, so the output will be like this:

Liputan 6 SCTV
SCTV

I used foreach, but always an error always happens.

1
  • 4
    Something like $your_variable['work'][0]['employer']['name']? Commented Jul 30, 2013 at 5:47

7 Answers 7

1

Try this:

foreach ($array['work'] as $arr) {
    echo $arr['employer']['name']."<br>\n";
}

This is assuming your data looks like:

$array = array(
    'work' => array(
        array(
            'employer' => array('id' => 111178415566505, 'name' => 'Liputan 6 SCTV'),
        ),
        array(
            'employer' => array('id' => 107900732566334, 'name' => 'SCTV'),
        ),
    ),
    'id' => 502163984,
);
Sign up to request clarification or add additional context in comments.

Comments

1

for instance your array variable name is $test, then you can get name value by

$test['work'][0]['employer']['name']

you can check your array structure using pre tag, like

 echo '<pre>';print_r($test);

Comments

1

You can use array_reduce, implode, and closure (PHP 5.3+) to do this.

echo implode("<br/>", array_reduce($array["work"],
    function(&$arr, $v){ 
        $arr[] = $v["employer"]["name"];
    },array()
));

1 Comment

this may very well be the most convoluted way to solve this, but its fun.
0

I assume $arr is working history array. You can use for Then array look like this :

for($i=0, $records = count( $arr['work']); $i < $records; $i++) {
    echo $arr['work'][$i]['employer']['name'] ."<br>";
}

Using foreach

foreach( $arr['work'] as $works) {
    echo $works['employer']['name'] ."<br>";
}

Comments

0
foreach ($your_array as $your_array_item)
{
    echo $your_array_item['work'][0]['employer']['name'] . '<br>';
}

Comments

0
$count=count($array);

for($i=0,$i<=$count;$i++){
echo $array['work'][$i]['employer']['name'];
}

This will work.. Dynamically..

1 Comment

Condition should be $i < $count.
0
foreach ($array['work'] as $val_arr) {
    echo $val_arr['employer']['name']."<br />";
}

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.