I need to create a array from another one that already exists, making a new data structure.
At this point I already have the new array
http://phpfiddle.org/main/code/x0gn-1ikz
The problem is:
At the new array in the 10:00:00 - 15:00:00 should apear 3 records:
Array
(
[10:00:00 - 15:00:00] => Array
(
[2014-08-03] => Jonh
[2014-08-04] => Jonh
[2014-08-04] => Lewis
)
But as you can see in the fidle only two of the apear, I belive this is a simple trick, but had blowed my head, the "problem" is in the records that have a date ans time equal....
Can I have some help please?
Change 114th line of your code to this:
$final[$time][$value[2]][] = $value[1];
to create array of names. print_r($final) will then print:
Array
(
[10:00:00 - 15:00:00] => Array
(
[2014-08-03] => Array
(
[0] => Jonh
)
[2014-08-04] => Array
(
[0] => Jonh
[1] => Lewis
)
)
Is that what you want?
Let's say array shown above is $table_array. Now to write name in column you use something like:
echo '<td>'.$table_array['10:00:00 - 15:00:00']['04-08-2014'].'</td>';
instead, you would write something like:
$names = '';
foreach ($table_array['10:00:00 - 15:00:00']['04-08-2014'] as $name)
$names .= $name . ', ';
$names = rtrim($names, ', ');
echo '<td>'.$names.'</td>';
If you do it different way then please post your code so I know how to help you
Your problem is duplicate key. For an instance, you try to push both jonh and lewis at the index of [10:00:00 - 15:00:00][2014-08-03] You need another array there, so the structure will be a 3D array.
One plausible structure looks like,
Array
(
[10:00:00 - 15:00:00] => Array
(
[2014-08-03] => Array
(
[0] => Jonh
)
[2014-08-04] => Array
(
[0] => Lewis
[1] => Jonh
)
)
Related
This question already has answers here:
Generate an associative array from an array of rows using one column as keys and another column as values
(3 answers)
Closed 7 months ago.
I have an array of array that looks like this:
Array
(
[0] => Array
(
[id] => I100
[name] => Mary
[gender] => F
)
[1] => Array
(
[id] => I101
[name] => John
[gender] => M
)
[2] => Array
(
[id] => I245
[name] => Sarah
[gender] => F
)
)
I want to set the key of the parent array with the value of id, so the result array looks like this:
Array
(
[I100] => Array
(
[id] => I100
[name] => Mary
[gender] => F
)
[I101] => Array
(
[id] => I101
[name] => John
[gender] => M
)
[I245] => Array
(
[id] => I245
[name] => Sarah
[gender] => F
)
)
If possible I'd like to avoid using an additional loop to go through the array and creating a new array to store each item with the proper key, as the array can have thousands of items.
Thanks in advanced!
Despite your caveat, a loop is the obvious solution:
$newArray = [];
foreach($oldArray as $item)
$newArray[$item['id']] = $item;
If the problem you have is not specifically with a loop, but rather creating a copy of the array is causes excessive memory consumption, then you can edit the array in place, with a for loop:
for($i=0; $i<count($oldArray); $i++){
$oldArray[$oldArray[$i]['id']] = $oldArray[$i];
unset($oldArray[$i]);
}
Note this works because the id elements are alphanumeric strings, if they where simple integars then the above code could overwrite sections.
The only other solution is to build the correct array in the 1st place, in a similar manner.
For example, using PDO::fetch instead of PDO::fetchAll:
//$newArray = $sth->fetchAll(PDO::FETCH_ASSOC);
$newArray = [];
while($row = $sth->fetch(PDO::FETCH_ASSOC))
$newArray[$row['id']] = $row;
You can't overwrite keys while iterating through an array "on the fly". So here is solution with array_map which produces an array with needed structure:
// assuming $arr is your initial array
$result = [];
array_map(function($a) use (&$result){
$result[$a['id']] = $a;
}, $arr);
// $result contains the needed array
You can add needed key during creation of this array.
My current project consist of multidimensional arrays in which it holds a date and some text contents.
i already used the normal arrays in my project and array_push is used for inserting an element to array. no i'm stuck in multidimensional array in which i don't know how to insert and display multi dimensional array(beginner to multi dimensional arrays).
i found a lot of results from stackoverflow itself, but none of them helped for me. i created a multidimensional array like this
$complaints = array(
$each_complaints => array(
"date" => "",
"text" => ""
)
);
then i want to add data's into this array on the loop of mysql result
<?php foreach($query_56 as $notes):
// eg: array_push " $notes->date , $notes->corresponding_text "
endforeach; ?>
and i want to display the array like this
array[date][text]=> [2014-11-18] [1st complaint]
array[date][text]=> [2015-01-15] [2nd complaint]
how can i achieve this, im begginer to multi dimesional arrays.
Any help would be greatly appreciated.
<?php foreach($query_56 as $key=>$notes):
$each_complaints[$key]["date"] = $notes->date;
$each_complaints[$key]["text"] = $notes->corresponding_text ;
endforeach;
echo "<pre>";print_r($each_complaints);
?>
**Output :**
(
[0] => Array
(
[date] => 01-11-2014
[text] => rrrrrr
)
[1] => Array
(
[date] => 02-11-2014
[text] => fffff
)
[2] => Array
(
[date] => 03-11-2014
[text] => ddddd
)
)
I got this array output in php:
Array
(
[blabla0] => Array
(
[0] => lalala
[1] => lelele
)
[blabla1] => Array
(
[0] => lalala
[1] => lelele
)
)
If I'd like to print by associative name, it's like this: $myArray['blabla0'][0] , and it works. But I want to do like this: echo $myArray[0][0], by index...is there any way?
Mark bakers answer is best for one option. You can convert all of the array with array_values eg
$new_array = array_values($old_array);
echo $new_array[0][0];
It depends how many times you wish to access the array
http://php.net/manual/en/function.array-values.php
I have an array which looks like this:
Array
(
[0] => Array
(
[1] => Array
(
[name] => vrij
// ...
)
[2] => Array
(
[name] => zat
// ...
)
)
)
I build this array using a for loop; however, I need to push 4 more 'records' to the array, which I can't do in this for loop.
I want the array to look like this, after the pushes:
Array
(
[0] => Array
(
[1] => Array
(
[name] => vrij
// ...
)
[2] => Array
(
[name] => zat
// ...
)
// ...
)
[1] => Array
(
[1] => Array
(
[name] => zon
//...
)
[2] // etc
)
)
The four new records should be pushed to array[1], so I get something like
$array[1][0], $array[1][1], etc. 0 1 2 3 contains the new data.
I tried quite a lot of stuff, to be honest. I need to do four of these pushes, so I was trying a for loop:
for($i = 0; $i < 4; $i++)
{
$day_info = $this->get_day_info($i, $data['init']['next_month'], $data['init']['current_year']);
$push['name'] = $day_info['day_name'];
array_push($data['dates'], $push);
}
and all other kinds of things with [], [1][$i], etc. Sometimes it even adds five arrays! I'm confused as to why it won't just add the [1][1], [1][2],.. I'm probably missing out on something here. Thanks a lot.
If this isn't clear, please do tell and I'll add more code to explain the problem better.
$extradates = array(1 => 'zon', 2 => 'maa');
$data['dates'][] = $extradates;
Will add 2 extra dates to the array using a new index.
Although if I see what you trying to accomplish, I think there might be a better way.
This above works though :)
How do I go about reformatting an object?
Array
(
[0] => Array
(
[user_id] => 5
[first_name] => Ace
[last_name] => Black
)
[1] => Array
(
[user_id] => 6
[first_name] => Gary
[last_name] => Surname
)
[2] => Array
(
[user_id] => 7
[first_name] => Alan
[last_name] => Person
)
)
I need to reformat this so the user_id name is changed to just 'id' and the first_name and last_name values are merged into a single value called 'name' so the final result would look like:
Array
(
[0] => Array
(
[id] => 5
[name] => Ace Black
)
)
You might find array_map useful for this
function fixelement($e)
{
//build new element from old
$n=array();
$n['id']=$e['user_id'];
$n['name']=$e['first_name'].' '.$e['last_name'];
//return value will be placed into array
return $n;
}
$reformatted = array_map("fixelement", $original);
As you can see, one downside is that this approach constructs a second copy of the array, but having written the callback, you can always use it 'in place' like this:
foreach($original as $key=>$value)
{
$original[$key]=fixelement($value);
}
If you want to do it in place:
Foreach that array, set the keys to the values you want, unset the keys for the values you no longer want.
If you want a copy of it:
Foreach that array and ETL (extract, transform, load) into another similar array.
If you need further details to help let me know.