I'm so tired of arrays today - thrown me all over the place.
So, here's the output of an array:
Array
(
[2010091907] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
Array
(
[2010091909] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
Array
(
[2010091901] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
I'm going to be writting a preg_match to iterate each [2010091907], but before I can, I do not understand how to get this piece of information, or how to call it. I'd be doing something like:
$json=json_decode($data,true);
foreach ($json['dont-know-what-to-call-it'] as $key => $value) {
echo "Key: ".$key."; Value: ".$value."<br />";
}
I just don't know how to call each one of those [2010091901] blocks, like what name I'm suppose to call them as. I know how to call the stuff under score, since it's named "score", and the data is under all that. I don't know how to get the key/value of the initial "sections" of the array. In the end, I'm going to want to grab each [2010091901], manipulate/use the data that is inbetween each one of the [2010091901], and then go to the next "record".
$date_keys = array_keys($json) would give (0 => 2010091907, 1 => 2010091909, ...) . then you could do
foreach ($date_keys as $d) {
foreach ($json[$d] as $key => $value) {
...
Also, if you do not actually need the indices of the outer array (the date values - 2010091907, etc) than you could just do
foreach ($json as $j) {
foreach ($j as $key => $value) {
...
ignoring the keys of $json
Can't you just nest foreach()s?
foreach($jsondata as $somedate => $value){
//do you actually need $somedate?
foreach($value['home']['score'] as $score){
echo $score.PHP_EOL;
}
}
You can just do
$json = json_decode($data, true);
foreach($json as $ymd => $data)
{
// $ymd = [2010091907, 2010091909,… ]
// $data is the array starting with the key home. so $data['home']['score'][1] = 7 for the first iteration
}
This answer your question? It's not 100% clear what you're asking
Related
This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 5 months ago.
Problem: I want to add the values of Array2 one-after-another to a multidimensional array (array1).
Array 1:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
)
)
Array2:
Array
(
[0] => 10
[1] => 15
[2] => 25
)
Goal:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 15
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 25
)
)
I am just learning programming since january 2021 so my best solution I got was this:
foreach ($array1 as $main => $value) {
foreach ($array2 as $sec => $entry) {
$array1[$main][] = $array2[$sec];
}
}
print_r($array1);
Output:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 10
[1] => 15
[2] => 25
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
)
What am I doing wrong in my loop?? How can I get the values of array2 one-by-one into array1??
Thanks for any help ;)
The issue with your code is that you have nested a loop within another when you do not need to. Instead, use the loop format foreach ($array1 as $key => $value) so you can capture its index in $key, then use that as an index to $array2.
foreach ($array1 as $key => $value) {
// Append from $array2 by its index $key
// Using [] will append at index [0]
// You can modify $array1 inside the loop if you
// target it by $key. Notice that we are not using
// $value at all in this loop - only the $key matters.
$array1[$key][] = $array2[$key];
}
print_r($array1);
foreach also allows you to modify the value directly if you use a & reference. This is a little bit shorter:
foreach ($array1 as $key => &$value) {
// Looping over references to $array1 sub-arrays
// means that $value can be modified directly
$value[] = $array2[$key];
}
print_r($array1);
Iteration by reference is described in the PHP foreach manual
How can I do to merge "artist" value with "title" value in one value?. I'm using PHP 5.2.4 if that helps.
I have this array
Array
(
[0] => Array
(
[artist] => Narcosis
[title] => Destruir
[duration] => 137
[date] => 1370807642
[genre_id] => 18
)
[1] => Array
(
[artist] => Ricardo Palma Fanjul
[title] => Mutant
[duration] => 347
[date] => 1448227909
[genre_id] => 18
)
)
The expected output would be like this, just show element "title" with twice values:
Array
(
[0] => Array
(
[title] => Narcosis - Destruir
[duration] => 137
[date] => 1370807642
[genre_id] => 18
)
[1] => Array
(
[title] => Ricardo Palma Fanjul - Mutant
[duration] => 347
[date] => 1448227909
[genre_id] => 18
)
)
Try this:
foreach ($mainArr as $index=>$subArr) {
$subArr['title'] = $subArr['artist'].' - '.$subArr['title'];
unset($subArr['artist']);
$mainArr[$index] = $subArr;
}
mainArr is the array declared above.
p.s. I have not tested this code yet
Try this:
$new = array_map(function($v) {
$v['title'] = $v['artist'].' - '.$v['title'];
unset($v['artist']);
return $v;
}, $arr);
OR
foreach ($arr as &$a) {
$a['title'] = $a['artist'].' - '.$a['title'];
unset($a['artist']);
}
I am trying to parse this array in order to save it in the database, but I can't do it.
This is what $this->input->post("question") produces:
Array
(
[1] => Array
(
[0] => question 1
[answer] => Array
(
[0] => answer 1
[1] => answer 2
[2] => answer 3
[3] => asnwer 4
[4] => answeer 5
)
)
[2] => Array
(
[0] => queston 2
[answer] => Array
(
[0] => answer 21
[1] => answere 22
[2] => anwer 23
[3] => answer 24
[4] => answer 25
)
)
)
I tried this:
foreach ($this->input->post("question") as $questions) {
foreach ($questions as $question) {
$data = array(
'question' => $question,
);
$this->db->insert('questions', $data);
$question_id = $this->db->insert_id();
//another foreach to go throw answers
}
}
Just to check if I save the questions correctly, but I am getting this message:
Message: Array to string conversion
First of all, you are getting that message because you are trying to convert an array to a string.
Second:
foreach ($this->input->post("question") as $questions) {
}
first level. After this, you remain with:
[1] => Array
(
[0] => question 1
[answer] => Array
(
[0] => answer 1
[1] => answer 2
[2] => answer 3
[3] => asnwer 4
[4] => answeer 5
)
)
foreach ($this->input->post("question") as $questions) {
foreach ($questions as $question) {
//here, $question is formed of 2 arrays
//[0] => question 1
//[answer] => Array
// (
// [0] => answer 1
// [1] => answer 2
// [2] => answer 3
// [3] => asnwer 4
// [4] => answeer 5
// )
}
}
that is the second level. so in that foreach, you should use:
$data = array(
'question' => $question[0], // to get the question string
);
Let me know if anything more is needed!
P.S: for answers, you have to go one level deeper, and make another foreach.
I have an array of dates that looks like this:
Array
(
[0] => '2014-01-01'
[1] => '2014-01-02'
[2] => '2014-01-03'
[3] => '2014-01-04'
[4] => '2014-01-05'
[5] => '2014-01-06'
[6] => '2014-01-07'
)
and I have another array of dates and counts that looks like this:
[All] => Array
(
[0] => Array
(
[count] => 2
[date] => 2014-01-06
)
[1] => Array
(
[count] => 1
[date] => 2014-01-03
)
[2] => Array
(
[count] => 43
[date] => 2013-12-11
)
[3] => Array
(
[count] => 103
[date] => 2013-12-10
)
[4] => Array
(
[count] => 128
[date] => 2013-12-09
)
[5] => Array
(
[count] => 75
[date] => 2013-12-08
)
[6] => Array
(
[count] => 107
[date] => 2013-12-07
)
I want to make a new associative array where all the keys are the dates from the first array above and all of the values are either the count matched up with the corresponding date or "0".
So for instance, the new array would look like this:
Array
(
[2014-01-01] => 0
[2014-01-02] => 0
[2014-01-03] => 1
[2014-01-04] => 0
[2014-01-05] => 0
[2014-01-06] => 2
[2014-01-07] => 0
)
Does that make sense? Please feel free to ask any questions you may have. Thank you!
Try this code:
$result = array();
foreach($firstArray as $f){
foreach($secondArray as $s){
if($s['date'] == $f) $result[$f] = $s['count'];
}
if(!array_key_exists($f, $result)) $result[$f] = 0;
}
$result = array();
foreach($secondArray as $s){
if(in_array($s['date'], $firstArray) {
unset($firstArray[$s['date']]);
$result[$s['date']] = $s['count'];
}
}
// if items left in first array that are not found within foreach:
if (!empty($firstArray))
$result = array_merge($result, array_fill_keys($firstArray, 0));
// sort by key so dates go ascending
ksort($result);
$new = array();
foreach($all as $row)
{
$new[$row['date']] = $row['count'];
}
array_merge ($new, $old);
Here $all is the array with the date and count indices.
$old is the exisisting array.
That's a 2-liner:
// use dates as index and set everything to 0
$result = array_fill_keys($x, 0));
// copy over existing counts
array_walk($all, function($v) use (&$result) { if (array_key_exists($v['date'], $result)) { $result[$v['date']] = $v['count'];}});
I have an array, let call it $mainArray, which looks like this: -
Array
(
[1] => Array
(
)
[5] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
[80] => Array
(
[0] => 20
[1] => 40
[2] => 50
[3] => 60
)
[777] => Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
[666] => Array
(
[0] => 1234
[1] => 5678
[2] => 20
[3] => 9865
)
[555] => Array
(
[0] => 111
[1] => 222
[2] => 333
[3] => 444
)
)
What I want to do is create 2 new arrays: -
1) Where values are equal to the key names of $mainArray, but only those where the sub-array (if there is one) contains the value "20" somewhere in it. For example my new array (call it $arrayOne) will be [0] => 5, [1] => 80, [2] => 666.
2) Similar to above, but where there's either no sub-array or, if there is, it doesn't include "20" as a value. So that (call it $arrayTwo) would be [0] => 1, [1] => 777, [2] =>555.
I've tried loads of for each loops and even a little RecursiveIteratorIterator (whatever that is!) but can't seem to reference keys and values in the way that I need to. Any help would be much appreciated!
Will this do?:
<?php
foreach( $mainArray as $mKey => &$mVal )
{
if( in_array( 20, $mVal ) )
{
$arrayOne[] = $mKey;
}
else
{
$arrayTwo[] = $mKey;
}
}
I trust you can create a function which would check if array contains 20 as it's value or not. Let's call this function has20.
You two new arrays would then be array_filter($mainArray, 'has20') and array_filter($mainArray, function ($x) {return !has20($x);})
You can do it like this:
$newArray = array();
foreach($mainArray as $key => $subArray) {
if (in_array(20, $subArray)) {
$newArray[] = $key;
}
}