I am having the following friendDetails array output,
Array
(
[0] => Array
(
[id] => 1
[first_name] => Aruun
[last_name] => Sukumar
[photo] => jpg
)
[1] => Array
(
[id] => 2
[first_name] => senthilkumar
[last_name] => Kumar
[photo] => jpg
)
)
I use the following piece of code to to get final output
foreach($friendDetails as $value){
array_push($friendList, $value[id].".".$value[photo]."-".$value[first_name]." ".$value[last_name]);
}
Final output will be,
Array
(
[0] => 1.jpg-Aruun Sukumar
[1] => 2.jpg-senthilkumar Kumar
[2] => 18.jpg-senthilkumar sugumar
)
Here I am getting Notice Error with exact output. What i done wrong on the code?
Is there any other way to get Final Output ?
You get the notice error as you are not putting the keys of your array in quotes.
It should be:
foreach($friendDetails as $value){
array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);
}
see http://php.net/manual/en/language.types.array.php
You need to put quotes around your key identifiers:
$value['id'] . "." . $value['photo']
etc. See "Why is $foo[bar] wrong?" at http://php.net/manual/en/language.types.array.php
Try this you will get both key and value:
foreach ($friendDetails as $key_name => $key_value) {
print "Key = " . $key_name . " Value = " . $key_value . "<BR>";
}
Two things:
You need to put quotes (") around the array keys in the array_push (i.e. $value["id"])
Make sure that you define $friendList as an array before the foreach.
A working example:
<?php
$friendDetails = array(
array(
'id' => 1,
'first_name' => 'Aruun',
'last_name' => 'Sukumar',
'photo' => 'jpg'
),
array(
'id' => 2,
'first_name' => 'senthilkumar',
'last_name' => 'Kumar',
'photo' => 'jpg'
)
);
$friendList = array();
foreach($friendDetails as $value){
array_push($friendList, $value["id"].".".$value["photo"]."-".$value["first_name"]." ".$value["last_name"]);
}
print_r($friendList);
?>
Use quotations around your array values:
foreach($friendDetails as $value){
array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);
}
$friendList = array();
foreach($friendDetails as $key=> $value){
$friendList[] = $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);
}
Related
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'];
}
}
?>
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>';
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 />";
}
I'm trying to concatenate an array, but when PHP comes across an empty array-item, it stops concatenating.
My array looks like this:
Array
(
[0] => Array
(
[0] => Test1
[1] => Test1
[2] =>
)
[1] => Array
(
[0] => Test2
[1] => Test2
[2] =>
)
[2] => Array
(
[0] => Test3
[1] => Test3
[2] => Test3
)
)
The 3th item on the first 2 Array-items are empty. And when I loop over them like this:
$keys = array('uid', 'type', 'some_column', 'other_column');
foreach ($csv as $i => $row) {
$uid = $row[0] . $row[1] . $row[2];
array_unshift($row, $uid);
$csv[$i] = array_combine($keys, $row);
}
I only get Test3Test3Test3 back, instead of the expected
Test1Test1
Test2Test2
Test3Test3Test3
So it looks like PHP is skipping items when concatenating an empty value.
Is this normal PHP behavior? And if so, how can I tackle this?
Try like
$uid = array();
foreach ($array as $i => $row) {
$uid[] = $row[0] . $row[1] . $row[2];
}
var_dump($uid);
Just you are giving $uid and it is taking it as an type variable and it appends the last occurance of loop into that variable.If you want your desired output you need to declare it as an array before.
I'm sorry, but if that is your desired output, you're overcomplicating things:
$foo = array(
array("Test1","Test1"),
array("Test2","Test2"),
array("Test3","Test3","Test3")
);
echo implode(PHP_EOL,
//implode all child arrays, by mapping, passes no delimiter
//behaves as concatenation
array_map('implode',$foo)
);
Returns:
Test1Test1
Test2Test2
Test3Test3Test3
In your case, you can use bits of this code like so:
$csv = array(array("Test1","Test1",''),array("Test2","Test2",''),array("Test3","Test3","Test3"));
$keys = array('uid', 'type', 'some_column', 'other_column');
foreach($csv as $k => $row)
{
array_unshift($row,implode('',$row));
$csv[$k] = array_combine($keys,$row);
}
gives:
Array
(
[0] => Array
(
[uid] => Test1Test1
[type] => Test1
[some_column] => Test1
[other_column] =>
)
[1] => Array
(
[uid] => Test2Test2
[type] => Test2
[some_column] => Test2
[other_column] =>
)
[2] => Array
(
[uid] => Test3Test3Test3
[type] => Test3
[some_column] => Test3
[other_column] => Test3
)
)
Try this:
$uid = array();
foreach ($array as $i => $row) {
$uid[] = $row[0] . $row[1] . $row[2];
}
var_dump($uid);
This outputs:
Array
(
[0] => Test1Test1
[1] => Test2Test2
[2] => Test3Test3Test3
)
You can do something similar to produce a string:
$uid = '';
foreach ($arr as $i => $row) {
$uid .= $row[0] . $row[1] . $row[2] . "\n";
}
echo $uid;
Output:
Test1Test1
Test2Test2
Test3Test3Test3
To get the expected output
$uid = "";
foreach ($array as $i => $row) {
$uid .= $row[0] . $row[1] . $row[2] . "\n";
}
echo $uid;
You need to use two foreach loop..
Procedure:
Initialize your first foreach loop to get the key => value as $key=> $value of each array inside.
Then initialize your second loop to $value variable since value inside of this is another array key => value = $innerKey => $innerValue.
Then inside your second loop echo $innerValue to display values inside of the secondary array
Lastly put an echo '<br/>' inside your first loop to put each secondary array into another newline.`
.
$data = array(
0 => array(
0 => 'Test1',
1 => 'Test1',
2 => ''
),
1 => array(
0 => 'Test2',
1 => 'Test2',
2 => ''
),
2 => array(
0 => 'Test3',
1 => 'Test3',
2 => 'Test3'
)
);
foreach($data as $key => $value){
foreach($value as $innerKey => $innerValue){
echo $innerValue;
}
echo '<br/>';
}
// Output would be..
Test1Test1
Test2Test2
Test3Test3Test3
Code Tested at : PHP Fiddle
I'm implementing memcached on a site and I'm caching the results of a specific query, which is working great, but I am having problems putting together the code to set the variables I need to make the cache usable.
My array is as follows, which contains two groups of data:
Array ( [0] => Array ( [0] => 9126 [id] => 9126 [1] => Oh penguin, you so silly. [title] => Oh penguin, you so silly. [2] => November-01-2011-00-14-09-ScreenShot20111031at9.jpg [path] => November-01-2011-00-14-09-ScreenShot20111031at9.jpg ) [1] => Array ( [0] => 9131 [id] => 9131 [1] => Reasons you die... [title] => Reasons you die... [2] => November-01-2011-00-17-04-ScreenShot20111031at8.jpg [path] => November-01-2011-00-17-04-ScreenShot20111031at8.jpg ) )
I can set them manually, and call them like this:
$id = $clean[0][0];
$title = $clean[0][1];
$path = $clean[0][2];
But I am having problems writing a WHILE loop to go through and set the variables dynamically. I also tried a FOR EACH statement to no avail:
for each($clean as $image){
$id = $image->id;
$path = $image->path;
$title = $image->title;
echo "THIS IS YOUR FREAKING ID $id THIS IS YOUR TITLE $title THIS IS YOUR PATH $path";
}
Any insight?
Edit:
Solution was to not call them as objects, as pointed out, change to reference them like this:
$id = $image["id"];
$path = $image["path"];
$title = $image["title"];
Cheers.
$array = array(
array ( 0 => 9126,
'id' => 9126,
1 => 'Oh penguin, you so silly.',
'title' => 'Oh penguin, you so silly.',
2 => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg',
'path' => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg' ),
array ( 0 => 9126,
'id' => 9126,
1 => 'Oh penguin, you so silly.',
'title' => 'Oh penguin, you so silly.',
2 => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg',
'path' => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg' )
);
foreach( $array as $row)
{
// Based on your array, you can either do:
echo $row['id'] . ' ' . $row['title'] . $row['path']. "\n";
echo $row[0] . ' ' . $row[1] . ' ' . $row[ 2 ] . "\n";
}
You could just serialize the entire array before saving in cache, then unserialize when you retrieve from cache. Then just reference the values as you indicated.
For each should serve you well. If you need to iterate through $clean as well just wrap around another foreach.
$arr = $clean[0];
foreach ($arr as $value) {
echo $value; // or in your case set
}