How to print specified array value - php

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.

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,
);

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);

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()
));

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>";
}

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

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

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

Related

How can I get a key value from two arrays on match?

I have 2 arrays, I'm trying to find any matches and return 'url from $array_full.
I tried array_intersect($array_full, $array_ids), but it doesn't work.
$array_full = array
(
Array
(
'#attributes' => Array
(
'topicid' => 102000,
'url' => 'Velkommen.htm',
'alias' => 'Velkommen'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130313,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130315,
'url' => 'SPedestal/Applikationer/LoadSharing/Indstillinger.htm',
'alias' => 'LOS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130312,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
)
);
$array_ids = array('130312', '130315');
I expect to get an array of matched url's, like:
array('WStation/WAS_Indstillinger.htm','SPedestal/Applikationer/LoadSharing/Indstillinger.htm')
A simple couple of foreach loops seems the easiest approach
$results = [];
foreach ( $array_full as $a ) {
foreach ( $a as $item ) {
if ( in_array($item['topicid'], $array_ids) ) {
$results[] = $item['url'];
}
}
}
print_r($results);
RESULT
Array
(
[0] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[1] => WStation/WAS_Indstillinger.htm
)
You will have to make foreach inside foreach to find item that is matching to ID.
Something like this (not tested, may contain some typos).
foreach($array_ids as $id) {
foreach($array_full as $key => $fullItem) {
if($fillItem['#attributes']['topicid'] != $id) {
continue;
}
//do what you need with $fullItem array
$key; // this is the key you want
}
}
you can use array_map, in_array to get the URL's
$result = [];
array_map(function($v) use ($array_ids,&$result){
$result[] = in_array($v['#attributes']['topicid'], $array_ids) ? $v['#attributes']['url'] : '';
}, $array_full);
Result:-
echo '<pre>';
print_r(array_filter($result));
Array
(
[2] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[3] => WStation/WAS_Indstillinger.htm
)

Getting a value from an associative array.

I'm a beginner and for some reason I'm having trouble with this one. I have the following associative array.
Array
(
[0] => Array
(
[name_type] => UnixName
[name] => charles
)
[1] => Array
(
[name_type] => DNSFQDN
[name] => charles.mydnsdomain.com
)
[2] => Array
(
[name_type] => DNSDomain
[name] => mydnsdomain.com
)
)
The problem is that these arrays are never in the same order and the keys are named the same. I only need the DNSFQDN. When I loop through the array how can I check to see if the DNSFQDN value is there, and then retrieve charles.mydnsdomain.com so I can put it into a varible.
I've tried functions like in_array and array_search but I'm having trouble with these as I'm working strictly with associative arrays.
Any help would be appreciated.
This will be helpful;
$input = array(
array(
'name_type' => 'UnixName',
'name' => 'charles'
),
array(
'name_type' => 'DNSFQDN',
'name' => 'charles.mydnsdomain.com'
),
array(
'name_type' => 'DNSDomain',
'name' => 'mydnsdomain.com'
)
);
$domain = $input[array_search('DNSFQDN', array_column($input, 'name_type'))]['name'];
echo $domain; exit;
// output; charles.mydnsdomain.com
its simple try bellow ..i hope it will help
<?php
$test_array = array(
array('name_type'=>'UnixName','name'=>'charles'),
array('name_type'=>'DNSFQDN','name'=>'charles.mydnsdomain.com'),
array('name_type'=>'DNSDomain','name'=>'mydnsdomain.com'),
);
foreach ($test_array as $key => $value) {
if($value['name_type']=='DNSFQDN'){
echo "Domain Name :";
echo $value['name'];
}
}
?>
You have to iterate array using foreach loop.
<?php
$array_values = array(
array('name_type'=>'UnixName','name'=>'charles'),
array('name_type'=>'DNSFQDN','name'=>'charles.mydnsdomain.com'),
array('name_type'=>'DNSDomain','name'=>'mydnsdomain.com'),
);
foreach ($array_values as $value) {
if($value['name_type']=='DNSFQDN'){
echo "DNSDomain = ".$value['name'];
}
}
?>

add item array in a exist array php [duplicate]

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
I need to add an item array in a exist array. When i try to add i get something like this, using array_push
Array
(
[0] => Array
(
[totalp] => 3.26
[mes] => Novembro
)
[1] => Array
(
[totalp] => 2.66
[mes] => Dezembro
)
[2] => Array
(
[0] => Array
(
[bonus] => 2.6
)
[1] => Array
(
[bonus] => 4.16
)
)
)
but i need this.
Array (
[0] => Array
(
[totalp] => 3.26
[mes] => Novembro
[bonus] => 2.6
)
[1] => Array
(
[totalp] => 2.66
[mes] => Dezembro
[bonus] => 4.16
) )
follow my code.
$arrRows = array();
while ($dados = $resultp ->fetch_array(MYSQLI_ASSOC)) {
$arrRows[] = $dados;//getting totalp and mes here
}
$arrRows1 = array();
while ($dados = $resultb ->fetch_array(MYSQLI_ASSOC)) {
$arrRows1[]=$dados; //getting bonus here
}
array_push($arrRows,$arrRows1); // first example, but i dont need this way.
print_r($arrRows);
thankyou
Use array_map
$res = array_map('array_merge', $arrRows, $arrRows1);
demo
<?php
$newdata = array (
0 => array('totalp'=>'3.26','mes' => 'Novembro'),
1 => array('totalp'=>'2.66','mes' => 'Dezembro')
);
foreach($newdata as $key => $value){
$newdata[$key]['Bonus'] = "12";
}
echo '<pre>';
print_r($newdata);
Check it On LIVE DEMO
Here is code that add bonus in multidimensional array
<?php
$array = array(
array(
"totalp" => "3.26",
"mes" => "Novembro"
),
array(
"totalp" => "3.26",
"mes" => "Novembro"
)
);
for ($i=0; $i < sizeof($array); $i++) {
$array[$i]['bonus'] = "2.2"; // here is you can get output from another array or variable
}
echo "<pre>";
print_r($array);
?>
See Output
Try this,
foreach($arrRows as $key=>$row){
$arrRows[$key]['bonus'] = $arrRows1[$key]['bonus'];
}
echo "<pre>";
print_r($arrRows);
echo "</pre>";
Use array merge like below code snippet:
$out = array();
foreach ($arrRows as $key => $value){
$out[] = array_merge((array)$arrRows1[$key], (array)$value);
}
print_r($out);
You can check this url for reference: Array merge on multidimensional array

CodeIgniter - How to print an array in the view?

sorry if the questions seams stupid but I have created an array in my controller and I pass it to the view with the $data[] array.
And I don't get it right to print the array with a for loop.
Can you please help me?
$measurearray = array(
'nr' => $answerNr,
'responsible' => $responsible,
'creationdate' => $InvDate2,
'activity' => $reason
);
$arr[$i] = $measurearray;
$data["failedMeasures_nr"] = $arr;
Output :
Array (
[50] => Array (
[0] => Array (
[nr] => 5d
[responsible] => werner
[creationdate] => asdfgdf
[activity] => appointed.
)
)
[73] => Array (
[0] => Array (
[nr] => 9g
[responsible] => 42887
[creationdate] => Zuzana
[activity] => r the training.
)
)
)
This is what happens when I print_r() it in the view.
My target is to print every single element for itself!
Have you tried this?
foreach ( $failedMeasures_nr[0] as $key => $val ) {
echo $key . ' is ' . $val;
}
With a foreach you can print separately the array elements
Please note that if you want to print more than the first index ( the 0 in my example ) you should use another foreach, resulting in something like this:
foreach ( $failedMeasures_nr as $new_arr ) {
foreach ( $new_arr as $key => $val ) {
echo $key . ' is ' . $val;
}
}
this way you will print the whole array separately
You can use foreach method to print each value.
<?php
foreach ($failedMeasures_nr as $print):
echo $print['nr'];
echo $print['responsible'];
echo $print['creationdate'];
echo $print['activity'];
endforeach;
?>
// controller page try this,
$data[$i]["failedMeasures_nr"] =array('nr' => $answerNr,
'responsible' => $responsible,
'creationdate' => $InvDate2,
'activity' => $reason);
$this->load->view('view_page',$data);
In View Page try this code,
for($i =0 ; $i <count($data);$i++){
foreach ($data[$i] as $key => $value) {
echo "$value";
echo "<br>";
}
}

Codeigniter PHP create two different array from one

I'm newbie of codeigniter and PHP.
Can I separate an array into two different arrays?
This is my $array:
Array (
[0] => Array
(
[Name] => mark
[Surname] => mark
)[1] => Array
(
[Name] => greg
[Surname] => greg
)
)
Is it possible to create an array of $mark and another with $greg?
If you want to use the value of Name as your variable name: Variable variables
foreach ($arrays as $array) {
if (isset($array['Name'])) {
$$array['Name'] = $array;
}
}
print_r($mark);
You may use eval() if you want to set a string value and make it a variable.
<?php
$arrays = array(
array(
'Name' => 'mark',
'Surname' => 'mark'
),
array(
'Name' => 'greg',
'Surname' => 'greg'
)
);
//I'd use foreach()
foreach ($arrays as $array) {
eval("$".$array['Name']." = array('Name'=>'{$array['Name']}','Surname'=>'{$array['Surname']}',);");
}
echo '<pre>';
var_dump($mark, $greg);
echo '</pre>';

Categories