I'm having a looping mind-breaking issue which I can't seem to solve myself. Currently working on saving a form input in a webshop. The data:
"personalisation" => array:3 [▼
0 => "embroidery"
1 => "printing"
2 => "embroidery"
]
"repeat" => array:2 [▼
0 => "true"
1 => "true"
]
"selectedColors" => array:1 [▼
0 => "3"
]
The problem which I have here: I need to loop through the personalisation array to add to my DB. With the embroidery, the repeat value is linked and for the printing the selectedColors is linked. How can I loop through the personlisation array and match the values from the other array?
I really wouldn't recommend designing forms like this, you're basically just sending a jumbled mess to your backend with no association.
You can "correct" the association by filtering the personalisation array and reindexing it so the keys match the other arrays.
$embroderies = array_values(array_filter($array['personalisation'], function($item) {
return $item === 'embroidery';
}));
foreach($emborderies as $key => $value) {
// get value from $array['repeat'][$key];
}
I can't think of any other way than to use a helper array for example.
It could be
array('embroidery' => 'repeat', 'printing' => 'selectedColors')
And you start looping through personalization, depending on the value you use it as a key in the helper array, then finally get the wanted value from the array.
1st iteration: 0/embroidery -> embroidery/repeat -> repeat/true
2nd iteration: 1/printing -> printing/selectedColors -> selectedColors/
...
Related
I need to compare arrays, if element from first or second array has duplicates in another one I need to exclude it. I know it sound simply and I'm sure it is but i cant handle with that problem :(
So i have first array like this:
Array:3 [
6 => blog/something
4 => blog/somethingElse
5 => blog/else
]
Second array almost identical:
Array:3 [
1 => /blog
2 => /comments
3 => /posts
]
And the last array:
(integer on the left is id of elements in second array, in this example
comments and posts)
Array:2 [
0 => array:2 [
'page_id' => 2
'value' => noindex
]
1 => array:2 [
'page_id' => 3
'value' => noindex
]
]
So if I have element in array first or second which exist in array thrid too AND have value = noindex i need to exclude it.
I have tried do this by foreach recursive, by array_walk_recursive but I still can't get satisfied result
First get all the indices you need to exclude and then exclude them:
$excludeIndices = array_column(array_filter($array3, function ($entry) {
return $entry['value'] === 'noindex';
}), 'page_id');
$keepArray1 = array_diff_key($array1, array_flip($excludeIndices));
$keepArray2 = array_diff_key($array2, array_flip($excludeIndices));
Sandbox
You can filter using the first two arrays directly.
$result = array_filter($last, function($item) use ($first, $second) {
return !($item['value'] == 'noindex' &&
(isset($first[$item['page_id']]) || isset($second[$item['page_id']]))
);
});
So I'm trying to change an array that I have;
+rows: array:31 [▼
0 => array:2 [▼
0 => "20190101"
1 => "5"
]
1 => array:2 [▼
0 => "20190102"
1 => "15"
]
2 => array:2 [▼
0 => "20190103"
1 => "17"
]
To modify the index 0 of each row to a Carbon DateTime Object for further processing.
Now I'd like to have a json output, but I currently even can't get rid of the Illegal offset type error....
The code I'm using to retreive the data (from GoogleAnalytics) is the follwing:
$get_stats = Analytics::performQuery(
Period::create(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()),
'ga:users',
[
'dimensions' => 'ga:date',
'samplingLevel' => 'HIGHER_PRECISION',
'include-empty-rows' => true
]
);
This returns alot of code, but I just grab the $get_stats->rows (see above for that output).
I then make a new array, defined as $stats = [];.
A loop iterates over the $get_stats->rows, so I can 'modify' the data in the array and append it to the newly created array.
foreach ($get_stats->rows as $value)
{
$stats[] = [Carbon::createFromFormat('Ymd', $value[0]) => $value[1]];
}
However I get (always) the Illegal offset type error, on the line with $stats[] = [Carbon::createFromFormat('Ymd', $value[0]) => $value[1]];
Can someone help me out with this? I have no clue why it does this, when I try to append an array to an array. I tried array_push(), gives the same issue, also array_merge has the same issue...
This is happening because Carbon::createFromFormat('Ymd', $value[0]) returns a Carbon object, and objects are not valid array indexes. I'm not sure what you're trying to achieve by doing this conversion on the key, it seems a simple
$stats[] = [$value[0] => $value[1]];
or even
$stats[$value[0]] = $value[1];
might be more useful to you. You can always do the conversion to a Carbon object when you want to display the data.
this might be a bit of a generic title, but I will try to explain my problem the best way I can.
So I've got this type of associative array:
Array
(
[ICO_0] => checked
[Investment_0] => checked
[Investment_1] => checked
[Marketing_1] => checked
)
What I would like to do is to divide it into multiple arrays based on numbers that are attached to the end of the key ( 0, 1 ... ). Basically I'd like to get a new array to look like this:
Array(
Array(
[ICO_0] => checked
[Investment_0] => checked
[Token Sale_0] => checked
),
Array(
[Investment_1] => checked
[Marketing_1] => checked
)
)
I've tried approaching this issue with array_chunk but couldn't get it to work.
I'll need this output since I want to sort those nested arrays based on items that they're holding, starting from the highest number of items.
Thanks!
You can use several different methods to accomplish this. One method is to loop through and explode the key name if you know there will always be an underscore, grab the last number, and use that as your index:
$results = [
"ICO_0" => "checked",
"Investment_0" => "checked",
"Investment_1" => "checked",
"Marketing_1" => "checked",
"Investment_2" => "checked",
"Marketing_2" => "checked"
];
foreach($results as $key => $value){
$ex = explode('_', $key);
$new_result[end($ex)][$key] = $value;
}
Which for me returns the following:
array:3 [▼
0 => array:2 [▼
"ICO_0" => "checked"
"Investment_0" => "checked"
]
1 => array:2 [▼
"Investment_1" => "checked"
"Marketing_1" => "checked"
]
2 => array:2 [▼
"Investment_2" => "checked"
"Marketing_2" => "checked"
]
]
I am trying to remove a key/value pair from an array but it does not seem to work. Basically, I make an API call which returns JSON. As such I do
$tempArray = json_decode($projects, true);
If I output $tempArray I see something like this
array:2 [
0 => array:9 [
"id" => 4
"name" => "Some Project Name"
"value" => "234"
"user_id" => "1"
"client_id" => "97"
"contact" => "Jane Berry"
]
1 => array:9 [
"id" => 3
"name" => "Another Project Name"
"value" => "6"
"user_id" => "1"
"client_id" => "97"
"contact" => "John Doe"
]
]
I essentially need to remove the value element so I do this
unset($tempArray['value']);
If I output $tempArray after the unset, it displays exactly the same as before, with the value element and value there.
What do I need to do to completely remove this from my array?
Thanks
unset will not look recursivly to sub-array to remove the key value. Only if you have at first level a key named value will be removed. In your array first level keys are: 0 and 1.
So to remove value from all sub-arrays you have to go throw all items from the array and unset it. You can do this with a simple foreach.
foreach($tempArray as $key => $data) {
unset($data['value']);
$tempArray[$key] = $data; //Overwrite old data with new with value unset.
}
Now you will not have value key in sub-array items.
As per my comment, you have no key called 'value' which is a top level key in your array. If you array looked like this:
$myArray = array(
"value" => "My Value to delete",
"anotherKey" => "hello world",
);
Then you could do unset($myArray['value']); and you would remove the key and value. In your case, the key you are looking for is nested under a numeric key [0] or [1]. You could reference these specifically like this:
unset($tempArray[0]['value']);
but what I imagine you are looking to achieve is to remove any trace of the key value from your array in which case you would be better off doing something like this:
foreach($tempArray as &$nestedArray){
unset($nestedArray['value']);
}
Note the & symbol before the $nestedArray. This means 'pass by value' and will actually update the $tempArray in a single line without the need for anything else.
Further Reading:
PHP Docs - Arrays
PHP Docs - Foreach loop
PHP Docs - Pass by reference
I have an array that can contain multidimensional elements. For populating checkboxes from database in the edit-form, I need to check for specific key-value pairs from this array.
The array ($areastools) can look like this:
array:9 [▼
15 => array:2 [▼
0 => 1
1 => 4
16 => 4
45 => array:2 [▶]
47 => 4
50 => array:2 [▶]
51 => 4
]
The first key is the specific area id ($area->pivot->id), and the values are the tool ids ($tool->id).
Currently I have this check, where the checkbox is checked if
(array_key_exists($area->pivot->id, $areastools) &&
($areastools[$area->pivot->id] == $tool->id))
This works for all elements of the array that are not multidimensional. The correct checkboxes are then checked in the edit-form.
But when two tools are added to the same area, the checkboxes for none of these tools are checked, as the condition does not check the values correct if an area has many tools.
I have been searching and trying, but can not find out how to do this. Can anyone please help me?
I'd make a little function. Not very elegant, but it works.
function areaTools($areaTools, $areaId, $toolId) {
if(array_key_exists($areaId, $areaTools) {
if(is_array($areaTools[$areaId]) {
foreach($areaTools[$areaId] as $tool) {
if($tool == $toolId;) {
return true;
}
}
} else {
return $areaTools[$areaId] == $toolId;
}
}
}
if you only want to know if a certain key-value pair is found, use array_walk_recursive()
$found=0;
function find_key_value($v, $k)
{
global $area,$tool,$found;
if (($k==$area->pivot->id)&&($v==$tool->id))
$found=1;
}
array_walk_recursive($areastools, 'find_key_value');