I have following array and i want to add "image" array inside that array(current array),How can i do this ?
here is my current array
Array
(
[0] => Array
(
[id] => 2
[first_name] => sandeep
[last_name] => sharma
)
[1] => Array
(
[id] => 3
[first_name] => Gaurav
[last_name] => Jain
)
And i want array something like this(want "image array" inside current array)
Array
(
[0] => Array
(
[id] => 2
[first_name] => sandeep
[last_name] => sharma
[image] =array
(
[0] => Array
(
[image] => abc.jpg
)
[1] => Array
(
[image] => abcdef.jpg
)
)
)
[1] => Array
(
[id] => 3
[first_name] => Gaurav
[last_name] => Jain
[image] =array
(
[0] => Array
(
[image] => abcdefghi.jpg
)
)
)
I tried with following code but its showing image array in last,i want image array inside current array,here is my code
$rows = $query->result_array(); //current array
array_push($rows, array("image"=>$image)); //$image is second array(where image save as array)
Try this
foreach ($rows as $i => $row) {
$rows[$i]['image'] = $image;
}
Please find the demo here
Simplest way is to loop your array :
foreach ($rows as $i => $row) {
// $i contains the index, here we push at the end the new array
$rows[$i][] = ['image' => $image];
}
Try this.
$rowsWithImages = array_map(function ($row) {
$row['image'] = $image;
return $row;
}, $rows);
print_r($rowsWithImages);
You can use foreach with pass by reference
foreach($a as &$_v){
$_v['image'] = $image;
}
Live DEMO : https://3v4l.org/VIgYP
Related
i have array strings variable like this
$qtybuy = array('233','235');
$dede = array('test1','test2');
$all = array('exchange' => $dede , 'sell' => $qtybuy);
and if i execute with print_r($all)
i got this code
Array
(
[exchange] => Array
(
[0] => test1
[1] => test2
)
[sell] => Array
(
[0] => 233
[1] => 235
)
)
and my problem is i want to get array like this
Array
(
[0] => stdClass Object
(
[exchange] => test1
[sell] => 233
)
[1] => stdClass Object
(
[exchange] => test2
[sell] => 235
)
)
maybe must use a foreach or loop but i dont know how to change array object with foreach or loop .. maybe someone can help me. and sorry for my bad english..
thanks
<?php
$qtybuy = array('233','235');
$dede = array('test1','test2');
$new = [];
foreach ($qtybuy as $key => $value) {
$new[] = (object)[
'exchange' => $dede[$key],
'sell' => $qtybuy[$key]
];
}
var_dump($new);
As you can see, you can just cast to object :)
I have an array that I'd like to restructure. I want to group items by turn. I can figure out how to extract data from the array using foreach($arr['history'] as $obj) my issue is with populating a new array using a loop.
Currently it looks like this:
Array (
[history] => Array (
[id] => 23452435
[legend] => Array (
[0] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => foo
)
)
[1] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => bar
)
)
[2] => Array (
[player] => opponent
[turn] => 1
[card] => Array (
[name] => derp
)
)
[3] => Array (
[player] => opponent
[turn] => 2
[card] => Array (
[name] => hoo
)
)
)
))
I want it to look like the following, but I can't figure out how to automatically create and populate this structure. This is an array with a sub-array for each turn, containing an array for me and opponent
Array (
[0] => Array (
[me] => Array (
[0] => foo
[1] => bar
)
[opponent] = Array (
[0] => derp
)
)
[1] => Array (
[me] => Array ()
[opponent] => Array (
[0] => hoo
)
))
Thanks.
Edit:
This is what I needed. Thanks for the answers.
$result = [];
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $list) {
$result[$list['turn']][$list['player']][] = $list['card']['name'];
}
}
Try this:
$result = [];
foreach ($data['history']['legend'] as $list) {
$result[$list['turn']-1][$list['player']][] = $list['card']['name'];
}
Fiddle it! http://ideone.com/BtKOKJ
You can just start adding data to the new array. PHP is extremely forgiving.
$historyByTurns = array();
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $legendItem) {
$turn = $legendItem['turn'];
$player = $legendItem['player'];
if (!array_key_exists($turn, $historyByTurns)) {
$historyByTurns[$turn] = array();
}
if (!array_key_exists($player, $historyByTurns[$turn])) {
$historyByTurns[$turn][$player] = array();
}
foreach ($legendItem as $card) {
$historyByTurns[$turn][$player][] = $card['name'];
}
}
}
You will have to test it, as I have no way to do that ATM.
I have a multidimentional array like this $membergroups :
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 100000251643877
[name] => Lathif Pambudi
[first_name] => Muhammad Lathif
[last_name] => Pambudi
[work] => Omah TI )
[2] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
How to delete one of child array with specific value. For the example, I want to delete an array with [id] => 100000251643877 inside? So the output will be :
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
Here is my php code, but it doesn't work :
if (($key = array_search($user_fbid, $membergroups)) !== false) {
unset($membergroups[$key]);
}
Any help would be greatly appreciated. Thank you
You can make use of array_column but only for php >= 5.5
if (($key = array_search($user_fbid, array_column( $membergroups, 'id') ) !== false) {
unset($membergroups[$key]);
}
array_column( $membergroups, 'id') search in membergroup multidimensional array for id column, and return tou you an array containing all rows values entries with id key.
array_column -> MANUAL
Using a foreach you can do the job like this
$id = 100000251643877;//Example
foreach($membergroups as $key => $value){
if($value['id'] == $id){
unset($membergroups[$key]);
}
}
Loop through the whole array:
foreach ($membergroups as $idx => $group) {
if ($group['id'] === $user_fbid) {
unset($membergrouops[$idx]);
break;
}
}
foreach($membergroups as $key => $value){
if($value['id'] == $user_fbid){
unset($membergroups[$key]);
}
}
Don't forget to merge the array after removing key to keep the index sequantially
$membergroups = array_merge($membergroups);
I have this array:
SimpleXMLElement Object
(
[id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
I need create some php function that help me group the values like this:
[0] => Array
(
[0] => Koala.jpg
[1] => koaladesc
[2] => 1
)
[1] => Array
(
[0] => Jellyfish
[1] => jelly desc
[2] => 5
)
Could anyone help me?
Something like this should do the trick, but it's localized to what you're asking based on the vagueness of your question:
$new_array = array();
foreach($simple_xml_object as $obj) {
if(is_array($obj)) {
for($i = 0; $i < count($obj); $i++) {
$new_array[$i][] = $obj[$i];
}
}
}
I would suggest looking at the documentation on the foreach() construct, as well as looking over the SimpleXML manual.
So, you want to tranpose an array. Here's a magical way of transposing rectangular arrays:
array_unshift($array, null);
$array = call_user_func_array('array_map', $array);
let's suppose your array is saved in variable $arrayValues
$arrayValues = [id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
now you need to create the following code:
foreach($arrayValues as $array)
{
echo $array->id;
echo $array->desc;
echo $array->qtidade;
}
this may work well for you.
username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}