Key Manipulation In PHP Array - php

So I have a function that merges two arrays replacing "variables" that are in the $template array with values from the $marketArray. It's nice tight bit of code from some kind contributor's here. Now due to a new requirement I need to switch things a bit.
First I need to make it an array of arrays that essentially groups the stuff by market instead of one giant list in a single large array. Secondly I need the keys in the new arrays to be of the format market-counter
e.g. gb/en-1, gb/en-2 etc etc (this is so a JQuery gets an id it can use to determine where the results go later.
So I have entered a couple of new entries (marked //NEW) that would get the value for the market and started a counter. It's twisting my brain around the next step that hurts!
$marketArray is a multidimensional associative array of the markets like this (but a lot more markets!)
$markets = array(
array(market => 'se/sv', storeid => 'storeId=66', langid => 'langId=-14', storenumber => '109', prodid => '741586', artid => '22112334'),
array(market => 'at/de', storeid => 'storeId=82', langid => 'langId=-11', storenumber => '234', prodid => '374637', artid => '45678214')
);
$template is a bunch of url templates that need to be manipulated on a market by market basis (again shortened)
$template = array (
'/$market',
'/$market/catalog/',
'/$marketproducts/$artid',
'StockSearchForm?&productId=$prodid'
);
Here is the function
function urlBuilder($marketArray,$template) {
$urlstohit=array();
foreach ($marketArray as $m) {
$market = $m['market']; //NEW
$counter = 1; //NEW
foreach ($template as $t) {
$tt=$t;
foreach ($m as $k=>$v)
$tt=str_replace('$'.$k, $v, $tt);
$urlstohit[]=$tt;
}
}
return ($urlstohit);
}
so what I am trying to achieve is instead of one giant array like
$urlstohit (
[0] => '/se/sv/catalog/categories/',
[1] => '/se/sv/catalog/news/',
[2] => '/se/sv/catalog/categories/departments/',
[3] => '/se/sv/search/?query=giant'
[4] => '/at/de/catalog/categories/',
[5] => '/at/de/catalog/news/',
[6] => '/at/de/catalog/categories/departments/',
[7] => '/at/de/search/?query=giant'
)
a md-array grouped by market with the market-counter as keys
$urlstohit (
['se/sv'] => array(
['se/sv-1'] => '/se/sv/catalog/categories/',
['se/sv-2'] => '/se/sv/catalog/news/',
['se/sv-3'] => '/se/sv/catalog/categories/departments/',
['se/sv-4'] => '/se/sv/search/?query=giant'
),
['at/de'] => array(
['at/de-1'] => '/at/de/catalog/categories/',
['at/de-2'] => '/at/de/catalog/news/',
['at/de-3'] => '/at/de/catalog/categories/departments/',
['at/de-4'] => '/at/de/search/?query=giant'
)
)

Try this
function urlBuilder($marketArray,$template) {
$urlstohit=array();
foreach ($marketArray as $m) {
$market = $m['market'];
$counter = 1;
$urlstohit[$market] = array(); / ADDED
foreach ($template as $t) {
$tt=$t;
foreach ($m as $k=>$v)
$tt=str_replace('$'.$k, $v, $tt);
$urlstohit[$market][$market.'-'.$counter]=$tt; // EDITED
$counter++; // ADDED
}
}
} // ADDED
return ($urlstohit);
}
I've marked the lines I've added and edited (I think you were also missing a curly brace).

Related

Remove similar from foreach

Trying use array_unique but this did not help, since not all fields are repeated for me.
My array:
'Rows' =>
array (
0 =>
array (
'HotelId' => 94852,
'OfferId' => 858080496,
'OfferIdStr' => '858080496',
'Price' => 2762,
),
1 =>
array (
'HotelId' => 94852,
'OfferId' => 858080497,
'OfferIdStr' => '858080497',
'Price' => 3000,
),
And my try:
$allHotels['Rows'] = array_unique($allHotels['Rows'], SORT_REGULAR);
Changed flags, didn't help either (SORT_NORMAL, etc...).
It deletes absolutely everything, i need to ignore the HotelId replay, only first need.
If you want to delete duplicated entries based solely on the HotelId value, it's probably easiest just to iterate over the array, storing the seen HotelId values and deleting the current entry if it's HotelId value has already been seen.
$seenHotels = array();
foreach ($allHotels['Rows'] as $key => $hotel) {
if (in_array($hotel['HotelId'], $seenHotels)) {
unset($allHotels['Rows'][$key]);
}
else {
$seenHotels[] = $hotel['HotelId'];
}
}
print_r($allHotels);
Demo on 3v4l.org

Replace value in multidimensional php array

First, I have to say I already checked some answers but neither it wasn't exactly what I was looking for neither I couldn't fully understand the answer and how to use it.
I have this MultiDimensional array:
Array
(
[field_5abcb693a68bc] => Array
(
[0] => Array
(
[field_5abcbb1b51ddf] => mortgage
[field_5ae58a0b58b58] =>
[field_5abcbb1e51de0] => 10
[field_5abcbb2051de1] => הידגלה
[field_5abcbb2351de2] => 45,654,456
[field_5abcbb6251de3] =>
[field_5abcbb6651de4] => 04/2017
[field_5abcbb6851de5] => 4,454,656
[field_5abcbb6b51de6] => 24/07/2018
[field_5abcbbb351de7] => 657
[field_5abcbbb651de8] => 24/07/2018
[field_5abcbbb851de9] => 15
[field_5abcbbbb51dea] => yes
)
)
)
And I want to find values that much the pattern of mm/yyyy. Regex is a good option, I know how to write the condition:
if ( preg_match('/^\d[1-9]\/[1-9][0-9][0-9][0-9]$/',$v) ) {
//do stuff
}
I want to look in the inner part of the array for this pattern, and if it is a match, to change the value in the array to
$value = '01/' . $value;
means the value '04/2017' changes to '01/04/2017'.
note: there can be more than one value to change.
note: the name of the first array within the array [field_5abcb693a68bc] can vary and won't stay the same in other cases.
Thanks.
Use array_walk_recursive with reference to value as argument instead of regular value argument of callback.
It would work like that:
array_walk_recursive(
$array,
function (&$value) {
if (preg_match('/^\d[1-9]\/[1-9][0-9][0-9][0-9]$/',$value)) {
$value = '01/' . $value;
}
}
);
Check result on 3v4l.org
You want to perform a conditional replacement using a regex pattern? preg_replace() seems sensible. preg_replace() is happy to iterate a one-dimensional array, so the lowest level can be directly served to it.
Notice that I've changed the pattern delimiter to avoid escaping the forward slash. I've also made the pattern more brief by using \d and the {3} quantifier. $0 means the "fullstring match". You don't have to write the unset() call to purge those temporary variables, but some developers consider it to be best practice because it avoids potential variable conflict down-script.
Code: (Demo)
$array = [
'field_5abcb693a68bc' => [
0 => [
'field_5abcbb1b51ddf' => 'mortgage',
'field_5ae58a0b58b58' => '',
'field_5abcbb1e51de0' => '10',
'field_5abcbb2051de1' => 'הידגלה',
'field_5abcbb2351de2' => '45,654,456',
'field_5abcbb6251de3' => '',
'field_5abcbb6651de4' => '04/2017',
'field_5abcbb6851de5' => '4,454,656',
'field_5abcbb6b51de6' => '24/07/2018',
'field_5abcbbb351de7' => '657',
'field_5abcbbb651de8' => '24/07/2018',
'field_5abcbbb851de9' => '15',
'field_5abcbbbb51dea' => 'yes'
]
]
];
foreach ($array as &$set) {
foreach ($set as &$subset) {
$subset = preg_replace('~^\d[1-9]/[1-9]\d{3}$~', '01/$0', $subset);
}
}
unset($set, $subset); // avoid future variable interferences
var_export($array);
Output:
array (
'field_5abcb693a68bc' =>
array (
0 =>
array (
'field_5abcbb1b51ddf' => 'mortgage',
'field_5ae58a0b58b58' => '',
'field_5abcbb1e51de0' => '10',
'field_5abcbb2051de1' => 'הידגלה',
'field_5abcbb2351de2' => '45,654,456',
'field_5abcbb6251de3' => '',
'field_5abcbb6651de4' => '01/04/2017',
'field_5abcbb6851de5' => '4,454,656',
'field_5abcbb6b51de6' => '24/07/2018',
'field_5abcbbb351de7' => '657',
'field_5abcbbb651de8' => '24/07/2018',
'field_5abcbbb851de9' => '15',
'field_5abcbbbb51dea' => 'yes',
),
),
)
You can anonymously process down the levels of the array without actually knowing any of the names of the keys like this for example
foreach ($arr as $key => &$outer) {
foreach ($outer as &$inner) {
foreach ($inner as $k => $v) {
if ( preg_match('/^\d[1-9]\/[1-9][0-9][0-9][0-9]$/',$v) ) {
$inner[$k] = '01/' . $v;
}
}
}
}

Restructuring Multi Dimensional Array Format

I am struggling with what would appear to be a pretty straight forward task. I have looked at and tried all kinds of functions and suggestion on SO hoping that maybe there is something simple and functional out there. Nothing I tried gives me the logic to do the restructuring.
I have a long complex array. However very much simplified the logic problem I am trying to solve generically is as follows:
$cost_type = Array
(
0 => "ISP2",
1 => "ISP3",
2 => "ISP4"
);
$supplier_name = Array
(
0 => "NAME-A",
1 => "NAME-B",
2 => "NAME-C"
);
$propertyid = Array
(
0 => "property1",
1 => "property2",
2 => "property2"
);
and I need to convert it to the following set of arrays (noting the concatenation of the two arrays with a common property id.....
$property1
(
array['charges']
[0] =>IPS2
array ['names']
[0] =>NAME-A
)
$property2
(
array['charges']
[0] ->IPS3
[1] =>IPS4
array['names']
[0] =>NAME-B
[1] =>NAME-c
)
I have tried everything over the course of the last few hours and a simple solution totally evades me.
If you can join the three arrays as you say in comments above this code will generate the look you want.
I loop through the array with property and keep key as the key to find names and charges in the other subarrays.
$cost_type = Array
(
0 => "ISP2",
1 => "ISP3",
2 => "ISP4"
);
$supplier_name =Array
(
0 => "NAME-A",
1 => "NAME-B",
2 => "NAME-C"
);
$propertyid = Array
(
0 => "property1",
1 => "property2",
2 => "property2"
);
$arr[] = $cost_type;
$arr[] = $supplier_name;
$arr[] = $propertyid;
$result = array();
Foreach($arr[2] as $key => $prop){
$result[$prop]["charges"][] =$arr[0][$key];
$result[$prop]["names"][] = $arr[1][$key];
}
Var_dump($result);
https://3v4l.org/EilvE
The following code converts the original array in the expected result:
$res = array();
foreach($arr[2] as $k => $foo){ // foreach property
if(!isset($res[$foo])){ // add property if not yet in list
$res[$foo] = array(
'charges' => array($arr[0][$k]),
'names' => array($arr[1][$k])
);
}else{ // add new value to already existing property
$res[$foo]['charges'][] = $arr[0][$k];
$res[$foo]['names'][] = $arr[1][$k];
}
}
Check it out here: https://eval.in/904473
Of course, it assumes a bunch on things about the data, but it should work for any number of items.
And if you need the property in another variable, just access it with $res['name of it].
Run this code you will get smiler result as you want :
$twodimantion=array();
$properties=array('property1','property2','property3');
$charges=array('ISP2','ISP3','ISP4');
$names=array('NAME-A','NAME-B','NAME-C');
foreach ($properties as $key => $property) {
$twodimantion['charge'][$key]=$charges[$key];
$twodimantion['names'][$key]=$names[$key];
$twoarray[$property]=$twodimantion;
}
echo '<pre>';
print_r($twoarray);
echo '</pre>';
I can't say I completely follow what you are trying to do, but I think this may be part of the way there for you.
When trying to restructure data in PHP, it's often helpful to create a empty array (or other data structure) to store the new data in first. Then you find a way to loop over your initial data structure that allows you to insert items into your reformatted structure in the right sequence.
<?php
$properties = []; // Array to hold final result
// Loop over your initial inputs
foreach ($groupsOfValues as $groupName => $groupValues) {
$temp = []; // Array to hold each groupings reformatted data
// Loop over the items in one of the inputs
for ($i=0; $i<count($group) && $i<count($properties)+1; $i++) {
if (!is_array($temp[$groupName])) {
$temp[$groupName] = [];
}
$temp[$groupName][] = $group[$i];
}
$properties[] = $temp;
}

PHP Array Check the attribute, return value based on attribute

I am trying to return pull a value based on an attribute from an array, and it seems straight forward enough but I can't seem to nail down the correct way to accomplish this.
Here is the array I am trying to pull from:
[1] => InfoOptions Object
(
[description] => INFO
[optSequence] => 2
[eqpObject] => CUSTOMER NTWK ENG
[attribute] =>
[eqpValue] =>
[dlrSequence] => 10
)
[2] => InfoOptions Object
(
[description] =>
[optSequence] => 3
[eqpObject] => CUSTOMER TEST
[attribute] => CUSTOMER
[eqpValue] => Jon Doe
[dlrSequence] => 10
)
Here is what I have so far:
if (is_array($provisionCVResult->path->infoOptions-_InfoOptions)) {
foreach ($provisionCVResult->path->infoOptions ->InfoOptions as $cv_obj) {
$CVA = array();
$result = null;
foreach ($CV_obj as $value) {
if($value['attribute'] == 'CUSTOMER') {
$CVA["eqpValue"] = $cv_obj->eqpValue;
break;
}
}
$this->cvArrayDataList[] = $CVA;
}
}
Where am I going wrong?
If $provisionCVResult->path->InfoOptions is an array, it does not make sense to write $provisionCVResult->path->InfoOptions ->InfoOptions in the foreach
EDIT: I red in the comments that the array is $provisionCVResult->path->InfoOptions->InfoOptions
PHP is case sensitive so $cv_obj and $CV_obj are two different variables
The second foreach is not needed
So, assuming $provisionCVResult->path->InfoOptions->InfoOptions is returning an array of InfoOptions Object, I think you should do something like this:
if (is_array($provisionCVResult->path->InfoOptions->InfoOptions))
{
$result = null;
foreach($provisionCVResult->path->InfoOptions->InfoOptions as $cv_obj)
{
if($cv_obj->attribute == 'CUSTOMER')
{
$this->cvArrayDataList[] = array("eqpValue" => $cv_obj->eqpValue);
}
}
}
Having a quick look, try changing
$value['attribute'] == 'CUSTOMER'
To
$value->attribute == 'CUSTOMER'
As the element is an "InfoOptions object" and not an array.
Note I would also recommend using strict comparison, e.g '===' instead of '=='.

Performing an action when finding same values in two PHP arrays

I have two associative arrays in PHP that are defined as following:
$this_week[] = array(
"top_song_id" => $row["view_song_id"],
"top_place" => $i,
"top_move" => "0",
"top_last" => $i,
"top_peak" => $i,
"top_rating" => get_song_rating_by_id($row["view_song_id"]),
"top_views" => $row["view_sum"],
"top_start" => $monday,
"top_end" => $sunday
);
and
$last_week[] = array(
"top_song_id" => $row["view_song_id"],
"top_place" => get_song_place_by_id($row["view_song_id"]),
"top_move" => "0",
"top_last" => get_song_last_by_id($row["view_song_id"]),
"top_peak" => get_song_peak_by_id($row["view_song_id"]),
"top_rating" => get_song_rating_by_id($row["view_song_id"]),
"top_views" => $row["view_sum"],
"top_start" => $prev_monday,
"top_end" => $prev_sunday
);
Now here is my question: how can I traverse this two arrays and perform an action if there is any song id in one array that can be found in the other one?
A for() loop doesn't work because there can be common songs for both arrays but not on the same array index.
Any help is appreciated.
An efficient way to do this is to just change the first line of the last snippet this way:
$last_week[$row["view_song_id"]] = array( // Added the song id as the array index
"top_song_id" => $row["view_song_id"],
...
After that you can use a simple for loop this way:
for ($this_week as $item) {
if ( isset ($last_week[ $item["top_song_id"] ]) ) {
// HERE YOU HAVE FOUND A DUPLICATE
}
}
if( in_array( $this_week["top_song_id"], $last_week ) ) {
//do something
}
Why not just hardcode the 5(?) comparisons you need in one if statement? No need to overcomplicate things.

Categories