I have the below array. I want to put condition dynamically as per the sections are coming.
stdClass Object
(
[content_form] => Array
(
[0] => genre
[1] => cast
[2] => category
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
[category] => Array
(
[0] => 4119
[1] => 4118
[2] => 4081
)
)
[conditions] => Array
(
[0] => OR
[1] => AND
)
)
In the above array for content_form there are 3 array values and in 0th index genre, 1st index cast and 2nd index category present.
Similarly for content_type there are 3 array values present and in the last array conditions present.
My requirement is that as per the section coming I want to put condition with them.
Example- ((genre OR cast) AND category)
similarly if my output is like below then the condition will be
Example - (genre OR cast)
[content_form] => Array
(
[0] => genre
[1] => cast
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
[0] => OR
)
)
And if my output is like the below then there will no condition with any other key and it will return the simple result.
stdClass Object
(
[content_form] => Array
(
[0] => cast
)
[content_type] => stdClass Object
(
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
)
)
How I can do this dynamically as per the sections are coming as per sequence with the array of content_type and conditions?
We have done this. Please check:
$jsonData = '{"content_form":["genre","cast","category", "prakash"],"content_type":{"genre":["History","ACTION","ROMANTIC"],"cast":["13128","13127"],"category":["4119","4118","4081"]},"conditions":["OR","AND","OR"]}';
$contentIds = array(
'genre'=>array(1,2,3),
'cast'=>array(1,2,4),
'category'=>array(3,2,7),
'prakash'=>array(1,2,3,8,9)
);
$arrayDecode = json_decode($jsonData,true);
$result = array();
foreach($arrayDecode['conditions'] as $key=>$val){
if($result){
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($result, $secondArr);
} else {
$firstArr = $contentIds[$arrayDecode['content_form'][$key]];
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($firstArr, $secondArr);
}
if($val=='OR'){
$result = array_unique($result);
} else {
$result = array_diff_assoc($result, array_unique($result));
}
}
$result = array_values($result);
print_r($result);
Let me know this is your requirement or not?
Related
Array
(
[0] => stdClass Object
(
[meta_id] => 23233
[post_id] => 4467
[meta_key] => first_name
[meta_value] => Daud
)
)
How can I echo post_id from this array for all posts using while or foreach statement?
Array
(
[classic-editor-remember] => Array
(
[0] => classic-editor
)
[_edit_lock] => Array
(
[0] => 1582905950:5
)
[_edit_last] => Array
(
[0] => 5
)
[_thumbnail_id] => Array
(
[0] => 4376
)
[slide_template] => Array
(
[0] => default
)
[_yoast_wpseo_content_score] => Array
(
[0] => 30
)
[_yoast_wpseo_primary_advisor_category] => Array
(
[0] =>
)
[title] => Array
(
[0] => Demo Daniel Wrenne, CFP, ChFC
)
[designation] => Array
(
[0] => Wrenne Financial Planing, LLC Lexington, KY
)
[client_specialities] => Array
(
[0] => Gen Y/Millennials, Medical Professionals
)
[address] => Array
(
[0] => 3223 S LEHI DR
)
[phone_number] => Array
(
[0] => 64646446486
)
[email_address] => Array
(
[0] => demo#demo.com
)
[website_url] => Array
(
[0] => a:3:{s:3:"url";s:23:"https://www.google.com/";s:4:"text";s:20:"View Advisor Profile";s:6:"target";s:4:"none";}
)
[first_name] => Array
(
[0] => Daud
)
[last_name] => Array
(
[0] => Yahya
)
)
And how can I get las_name, first_name, email, address, website url, specialities, designation and title from the above array using and loop like while or foreach loop.
This is less a WordPress question and a basic PHP foreach question.
The first example you have is an Object, so you need to access the properties, e.g. meta_id, post_id like:
// THIS IS JUST AN EXAMPLE. YOUR VARIABLE WILL CHANGE BASED ON HOW YOU GOT THE DATA. `$object_array` is how you got the data to begin with.
foreach( $object_array as $object ) {
$post_id = $object->post_id;
echo $post_id;
}
For your second example, since there is only one array key inside each array key, you would set it up like this:
// Example. you would use whatever you used to get the array to begin with as the `$array`.
foreach ($array as $item ) {
$last_name = $item['last_name'][0];
$first_name = $item['first_name'][0];
....
}
I have an array, which is given below:
$test = Array
(
[0] => Array
(
[0] => stud 1
)
[1] => Array
(
[0] => stud 2
)
[2] => Array
(
[0] => stud 3
)
);
I want to add a common element to above array with out using loop. For example, I want to add "test" to each element of array. After adding "test", array will look like:
$test = Array
(
[0] => Array
(
[0] => stud 1
[1] => 'test'
)
[1] => Array
(
[0] => stud 2
[1] => 'test'
)
[2] => Array
(
[0] => stud 3
[1] => 'test'
)
);
Is there any way to add common element array with out using any kind of loop(for, foreach etc...)?
You can use array_map(), check the live demo
array_map(function($v){$v[] = 'test'; return $v;}, $array);
I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}
OK, so I got a while loop, in which I loop through different users:
$before = $data['autorenew_before'];
$refs=$dbh->prepare("SELECT * FROM users WHERE user_by=:userby AND expire <= unix_timestamp(CURRENT_TIMESTAMP + INTERVAL :before day)");
#$refs->bindParam(":userby",$data['username']);
$refs->bindParam(":userby",$userdata['username']);
$refs->bindParam(":before",$before);
$refs->execute();
I then loop through the above query:
while($refsData=$refs->fetch()){
$ids = "".$refsData['id'].",";
$explode = explode(",",$ids);
$outcome = _paying(number_format(getPriceList($data['rented_referrals']),2), 30, $data['username'], $explode);
}
This is the _paying function:
function _paying($ceny, $dni, $username, $referrals_array){
//$ceny = 0.20
//Count referrals_array doesn't return anything.
$koszyk = $ceny * count($referrals_array);
return $koszyk;
}
The above function doesn't work, as the count($referrals_array) is not working.
Edit - 1:
print_r($explode); gives me:
Array
(
[0] => 40231
[1] =>
)
Array
(
[0] => 40232
[1] =>
)
Array
(
[0] => 40233
[1] =>
)
Array
(
[0] => 40234
[1] =>
)
Array
(
[0] => 40235
[1] =>
)
Array
(
[0] => 55847
[1] =>
)
Array
(
[0] => 55848
[1] =>
)
Array
(
[0] => 90322
[1] =>
)
Array
(
[0] => 90323
[1] =>
)
Array
(
[0] => 90324
[1] =>
)
Array
(
[0] => 90325
[1] =>
)
Array
(
[0] => 90326
[1] =>
)
What am I doing wrong?
Have you checked the content of $explode yet?
Try a print_r($explode); before calling _paying() to make sure, there is data.
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);