So I'm trying to figure out this small piece, but I'm completely stuck and could use some assistance. I will break the code down into sections for understanding.
This will grab 5 of my posts:
$post_list = get_posts([
'post_type' => 'post',
'posts_per_page' => 5,
]);
Output per post:
Array
(
[0] => WP_Post Object
(
[ID] => 59343
)
)
Then I'm adding the returned ID's into an array using the below code:
$post_authors = [];
foreach ($post_list as $key => $post) {
$post_author = \get_post_author($post->ID);
$post_authors[] = $post_author['ID'];
}
print_r($post_authors);
This outputs me the following array:
Array
(
[0] => 52714
[1] => 0
[2] => 3339
[3] => 0
[4] => 0
)
Then I'm matching my custom post author meta associated with the post:
$test_id = [];
foreach ($post_authors as $author) {
$test_id[] = get_post_meta($author, '_test_id');
}
print_r($test_id);
This is the output that I get:
Array
(
[0] => Array
(
[0] => 7899
)
[1] =>
[2] => Array
(
[0] => 3487
)
[3] =>
[4] =>
)
Is there a way that I can map the above posts as keys and then have the id as outputs? Also skip the empty values on get_posts().
For example:
Array
(
[52714] => 7899
[3339] => 3487
)
All help would be appreciated!
If I understand your question correctly then you can use this logic.
$posts = array(5676,6767,0,0,564,898);
$test_id = [];
foreach ($posts as $key =>$value) {
$test_id[$value] = $key; // get_post_meta($author, '_test_id');
}
print_r($test_id);
//output
Array
(
[5676] => 0
[6767] => 1
[0] => 3
[564] => 4
[898] => 5
)
Related
I created an array that has values that are in groups of five, but I can't seem to figure out how to fetch them individually as I must in order to use them in calling another function.
Sample array contains:
Array
(
[0] => Array
(
[0] => Your Full Name
[1] => Name
[2] => 1
[3] =>
[4] => 50
)
[1] => Array
(
[0] => Your Email
[1] => EMail
[2] => 1
[3] =>
[4] => 50
)
[2] => Array
(
[0] => Message
[1] => Message
[2] => 5
[3] => 5
[4] => 50
)
)
The PHP snippet for using it:
$Values = array_chunk($Values, 5);
foreach ($Values as $row) :
foreach ($row as $key) :
valueTypes($key[0], $key[1], $key[2], $key[3], $key[4], "db_name");
endforeach;
endforeach;
You're doing it wrong. You need only one foreach.
Snippet
$Values = array_chunk($Values, 5);
foreach ($Values as $key) :
valueTypes($key[0], $key[1], $key[2], $key[3], $key[4], "db_name");
endforeach;
Explanation:
Your array is multidimensional array, if you use two nested foreach , $key becomes string, and $key[0](Key Index) have no data.
I'm currently looking for a solution to this problem:
if I have something like that:
Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
[3] => Array
(
[id] => 1
[name] => Paper
)
[4] => Array
(
[id] => 4
[name] => Puppy
)
)
The goal I'd like to reach here is create a new array which contains the arrays with the same ID. So basically, at the end I'm gonna have two arrays: the first will contain element with different IDs and the second will contain element with the same ID.
Any tips? Thank you in advance!
One way is by mainly using array_filter()
// Gather ids and count
$id = array_count_values(array_column($array, 'id'));
// Filter not unique
$notUnique = array_filter($array, function($e) use ($id) {
return ($id[$e['id']] > 1);
});
// Filter unique
$unique = array_filter($array, function($e) use ($id) {
return !($id[$e['id']] > 1); // or ($id[$e['id']] == 1)
});
// Print result
print_r($notUnique);
echo '<br>';
print_r($unique);
Try this
<?php
$array = array(
array('id' => 1,'name' => 'Timer'),
array('id' => 2,'name' => 'Tub'),
array('id' => 1,'name' => 'Paper'),
array('id' => 4,'name' => 'Puppy')
);
$new = array();
foreach($array as $r)$new[$r['id']][] = $r['name'];
echo '<pre>';print_r($new);
?>
Output
Array
(
[1] => Array
(
[0] => Timer
[1] => Paper
)
[2] => Array
(
[0] => Tub
)
[4] => Array
(
[0] => Puppy
)
)
I have an array that I'd like to restructure. I want to group items by turn. I can figure out how to extract data from the array using foreach($arr['history'] as $obj) my issue is with populating a new array using a loop.
Currently it looks like this:
Array (
[history] => Array (
[id] => 23452435
[legend] => Array (
[0] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => foo
)
)
[1] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => bar
)
)
[2] => Array (
[player] => opponent
[turn] => 1
[card] => Array (
[name] => derp
)
)
[3] => Array (
[player] => opponent
[turn] => 2
[card] => Array (
[name] => hoo
)
)
)
))
I want it to look like the following, but I can't figure out how to automatically create and populate this structure. This is an array with a sub-array for each turn, containing an array for me and opponent
Array (
[0] => Array (
[me] => Array (
[0] => foo
[1] => bar
)
[opponent] = Array (
[0] => derp
)
)
[1] => Array (
[me] => Array ()
[opponent] => Array (
[0] => hoo
)
))
Thanks.
Edit:
This is what I needed. Thanks for the answers.
$result = [];
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $list) {
$result[$list['turn']][$list['player']][] = $list['card']['name'];
}
}
Try this:
$result = [];
foreach ($data['history']['legend'] as $list) {
$result[$list['turn']-1][$list['player']][] = $list['card']['name'];
}
Fiddle it! http://ideone.com/BtKOKJ
You can just start adding data to the new array. PHP is extremely forgiving.
$historyByTurns = array();
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $legendItem) {
$turn = $legendItem['turn'];
$player = $legendItem['player'];
if (!array_key_exists($turn, $historyByTurns)) {
$historyByTurns[$turn] = array();
}
if (!array_key_exists($player, $historyByTurns[$turn])) {
$historyByTurns[$turn][$player] = array();
}
foreach ($legendItem as $card) {
$historyByTurns[$turn][$player][] = $card['name'];
}
}
}
You will have to test it, as I have no way to do that ATM.
I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}
Hi I have one multidimensional array, I retrieved the data using mysql PDO and formatted it this way
array('id'=>$row['empname'],'data'=>array(array($row['salestat'],$row['salecount'])))
Array
(
[0] => Array
(
[id] => 'employee1'
[data] => Array
(
0 => 'Sold'
1 => 1
)
)
[1] => Array
(
[id] => 'employee2'
[data] => Array
(
0 => 'On Process'
1 => 4
)
)
[2] => Array
(
[id] => 'employee2'
[data] => Array
(
0 => 'Sold'
1 => 2
)
)
)
I am trying to make my output like this, trying to format this so I can use it for drilldown series in highcharts, thank you
Array
(
[0] => Array
(
[id] => 'employee1'
[data] => Array
(
0 => 'Sold'
1 => 1
)
)
[1] => Array
(
[id] => 'employee2'
[data] => Array
[0]
(
0 => 'On Process'
1 => 4
)
[1]
(
0 => 'Sold'
1 => 2
)
)
)
Firstly, you can not use a field inside an array twice with the same key like salescount or salestat. But you can do something like this.
function wrapSameId( $employeeArray ) {
//create the new array.
$newEmployeeArray = array();
//loop the old array.
foreach ($employeeArray as $key => $value) {
$exists = 0;
$i=0;
$id = $value['id'];
$data = $value['data'];
//see if you can find the current id inside the newly created array if you do, only push the new data in.
foreach ($newEmployeeArray as $key2 => $value2) {
if( $id == $value2['id'] ) { $newEmployeeArray[$key2]['data'][] = $data;$exists = 1;var_dump($value2['data']); }
$i++;
}
//if we didnt find the id inside the newly created array, we push the id and the new data inside it now.
if( $exists == 0 ){
$newEmployeeArray[$i]['id'] = $id;
$newEmployeeArray[$i]['data'][] = $data;
}
}
return $newEmployeeArray;
}
This function should return a new Array but if in the old array there were multiple arrays with the same id, in this one we should have:
Array
(
[0] => Array
(
[id] => employee1
[data] => Array
(
[0] => Array
(
[salescount] => 4
[salesstat] => Sold
)
)
)
[1] => Array
(
[id] => employee2
[data] => Array
(
[0] => Array
(
[salescount] => 2
[salesstat] => In Progress
)
[1] => Array
(
[salescount] => 5
[salesstat] => Sold
)
)
)
)
You can use it like this:
$employees = wrapSameId( $PDOArray );
where $PDOArray is your initial Array. Hope this is what you were looking for.
Why not index your output array by id?
Loop over your initial array and add the salecount, salestat pair to an employee's data array.
$outputArray = array();
//Loop over each entry pulled from you database...
foreach ($employeeArray as $employeeInfo)
{
//If no index for the current employee exists, create an empty array
//The only reason I'm including this is because I'm not sure how array_merge handles nulls -- just to be safe
if (!$outputArray[$employeeInfo['id']])
{
$outputArray[$employeeInfo['id']] = array();
}
//Merge the existing array for that employee with the new data for the same employee
$outputArray[$employeeInfo['id']] = array_merge($outputArray[$employeeInfo['id']], $employeeInfo['data']);
}
print_r($outputArray);
It's easier to visualize the output...
Array
(
[employee1] => Array
(
0 => 1
1 => 'Sold'
)
[employee2] => Array
(
0 => 4
1 => 'On Process'
2 => 2
3 => 'Sold'
)
)
If you're not satisfied with your output being indexed by id, loop over it again and format it as you want.
$otherOutputArray = array();
foreach ($outputArray as $key => $value)
{
$otherOutputArray[] = array('id' => $key, 'data' => $value);
}
That gives you exactly what you want.