PHP Unset parent if not found in an array - php

I'm having an issue unsetting my parent if I do not find my needle in my child, for some reason I just cannot get this to work no matter how many different ways I try it, could someone possibly point me in the right direction? This is being thrown at a multidimensional array with a floor depth of about 4. Here's the code and an example of what a slice of the array might look like. In this case only Array[3] should remain, Array's [1-2] should be removed.
array (
1 =>
array (
197015 =>
array (
345415 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213354 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
2 =>
array (
197014 =>
array (
345414 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213353 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
3 =>
array (
197013 =>
array (
345412 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => 'Jedi',
'products_quantity' => '1',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
345413 =>
array (
'options' =>
array (
'' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
Here's a sample of the last code I tried using without success, if you replace unset($k); with return false; it will return which secondary array the needle appears in.
// array_search with recursive searching, optional partial matches and optional search by key
function array_find_r($needle, &$haystack, $partial_matches = false, $search_keys = false) {
if(!is_array($haystack)) return false;
foreach($haystack as $key=>&$value) {
$what = ($search_keys) ? $key : $value;
if($needle===$what) return $key;
else if($partial_matches && #strpos($what, $needle)!==false) return $key;
else if(is_array($value) && array_find_r($needle, $value, $partial_matches, $search_keys)!==false) return $key;
}
unset($k);
}
$tty = array();
foreach($datas as &$k) {
$tty[] = array_find_r('Jedi', &$k, true, false);
}
$tty=array_filter($tty); rsort($tty);
echo '<pre>'; var_export($datas); echo '</pre>';

Why not push the data you do need into a new array and unset the whole old array?

Related

How to output value of an array

I am a complete beginner in PHP. However I know how to output the value of custom field. I am having a bit of problems with arrays. The post meta key is fw_options. The value has multiple arrays and looks like this:
array (
0 =>
array (
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array (
'time' => 'fixed',
'hourly' =>
array (
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array (
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>
array (
),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array (
0 => '717',
),
),
)
I have this code which I have tried with no success:
$array = get_post_meta( get_the_ID(), 'fw_options', true );
echo $array[0]['reading_type']['fixed']['reading_times'];
How can I output the value 500 from the post meta key reading_times?
Simple Print array according to key
First hold the value into a variable than print according to array key
$data = array(
'featured_post' => false,
'featured_expiry' => '',
'_featured_book_string' => '0',
'reading_level' => 'medium',
'reading_type' =>
array(
'time' => 'fixed',
'hourly' =>
array(
'hourly_read' => '',
'estimated_hours' => '',
),
'fixed' =>
array(
'reading_times' => '500',
),
),
'reading_duration' => 'one_month',
'english_level' => 'fluent',
'readers_level' => 'starter',
'expiry_date' => '2019/12/31',
'show_attachments' => 'off',
'read_documents' =>array(),
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array(
0 => '717',
),
);
echo $data['reading_type']['fixed']['reading_times'];
Output #500

Get values from unserialized data

I have a serialized field in my database. I can get the contents of this field and unserialize them however I am unsure how to get certain values of these. I ultimately need a foreach of each item and value from the data.
In the below example I need to be able to get the following:
Main Image Replacement:
BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG Complimentary:
Comp Text Quote Code: Code Text
I need the label as one variable and the value as another within a foreach. These labels and values are variable so I cannot manually get this data by their labels.
array (
0 =>
array (
'mode' => 'builder',
'cssclass' => '',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'radio',
'rules_type' =>
array (
'Reprint_0' =>
array (
0 => '',
),
'Edit Artwork_1' =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => '',
'value' => 'Edit Artwork',
'price' => '',
'section' => '58073632e582b5.35893028',
'section_label' => '',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
'GBP' => '',
),
'quantity' => 1,
'multiple' => '1',
'key' => 'Edit Artwork_1',
'use_images' => '',
'changes_product_image' => '',
'imagesp' => '',
'images' => '',
),
1 =>
array (
'mode' => 'builder',
'cssclass' => '',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'radio',
'rules_type' =>
array (
'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG_0' =>
array (
0 => '',
),
'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-21.JPG_1' =>
array (
0 => '',
),
'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-77.JPG_2' =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => 'Main Image Replacement',
'value' => 'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG',
'price' => '',
'section' => '58073632e582d2.46631826',
'section_label' => 'Main Image Replacement',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
'GBP' => '',
),
'quantity' => 1,
'multiple' => '1',
'key' => 'BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14.JPG_0',
'use_images' => 'images',
'changes_product_image' => '',
'imagesp' => '',
'images' => 'http://burhill.immaculate.co.uk/wp-content/uploads/2016/10/BGL_Burhill_People_AndyHiseman_300dpi_Super-Size-14-150x150.jpg',
),
2 =>
array (
'mode' => 'builder',
'cssclass' => 'col-6',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'textfield',
'rules_type' =>
array (
0 =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => 'Complimentary',
'value' => 'Comp Text',
'price' => '',
'section' => '58073632e582f4.32183997',
'section_label' => 'Complimentary',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
),
'quantity' => 1,
),
3 =>
array (
'mode' => 'builder',
'cssclass' => 'col-6',
'hidelabelinorder' => '',
'hidevalueinorder' => '',
'element' =>
array (
'type' => 'textfield',
'rules_type' =>
array (
0 =>
array (
0 => '',
),
),
'_' =>
array (
'price_type' => '',
),
),
'name' => 'Quote Code',
'value' => 'Code Text',
'price' => '',
'section' => '58073632e58317.46363272',
'section_label' => 'Quote Code',
'percentcurrenttotal' => 0,
'currencies' =>
array (
),
'price_per_currency' =>
array (
),
'quantity' => 1,
),
)
foreach($your_array as $key => $value) {
$your_value = $value['value'];
$your_label = $value['section_label'];
}
This should work for you as long as i got the right keys there.

PHP - Creating an array from CSV and then echo the values

I am attempting to parse CSV strings into a multi-dimensional array. I am using the following code to do this...
public function exportPartsAuthority($fileArray)
{
foreach ($fileArray as $filename => $fileContent) {
$lines = explode("\n", $fileContent);
$formatting = explode(",", $lines[0]);
unset($lines[0]);
$results = array();
foreach ( $lines as $line ) {
$parsedLine = str_getcsv( $line, ',' );
$result = array();
foreach ( $formatting as $index => $caption ) {
if(isset($parsedLine[$index])) {
$result[$formatting[$index]] = trim($parsedLine[$index]);
} else {
$result[$formatting[$index]] = '';
}
}
$results[] = $result;
}
$var_str = var_export($results, true);
$var = "<?php\n\n\$$values = $var_str;\n\n?>";
file_put_contents('/home/apndev/public_html/output.txt', $var);
}
}
The end result is this:
$ = array (
0 =>
array (
'Ordernumber' => '100000002',
'Orderdate' => '02/10/2013',
'OrderStatus' => 'pending',
'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
'PaymentMethod' => 'checkmo',
'ShippingMethod' => 'flatrate_flatrate',
'Subtotal' => '2.0000',
'ShippingCost' => '10.0000',
'GrandTotal' => '12.0000',
'TotalTax' => '0.0000',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => 'K&N Air Filter Wrap',
'ItemSKU' => 'YA-6504PK K&N',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '1.0000',
'CostPrice' => '',
'ItemOrdered' => '1.0000',
'ItemInvoiced' => '0.0000',
'ItemSent' => '0.0000',
'CustomerID' => '1',
'BillingFirstName' => 'Brian',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => 'Florida',
'BillingCountry' => 'US',
'ShippingFirstName' => 'Brian',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => 'Florida',
'ShippingCountry' => 'US',
'Vendor SKU' => '',
'Line Code
' => '',
),
1 =>
array (
'Ordernumber' => '100000002',
'Orderdate' => '02/10/2013',
'OrderStatus' => 'pending',
'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
'PaymentMethod' => 'checkmo',
'ShippingMethod' => 'flatrate_flatrate',
'Subtotal' => '2.0000',
'ShippingCost' => '10.0000',
'GrandTotal' => '12.0000',
'TotalTax' => '0.0000',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => 'K&N Air Filter Wrap',
'ItemSKU' => 'YA-6601-TDK K&N',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '1.0000',
'CostPrice' => '',
'ItemOrdered' => '1.0000',
'ItemInvoiced' => '0.0000',
'ItemSent' => '0.0000',
'CustomerID' => '1',
'BillingFirstName' => 'Brian',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => 'Florida',
'BillingCountry' => 'US',
'ShippingFirstName' => 'Brian',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => 'Florida',
'ShippingCountry' => 'US',
'Vendor SKU' => '',
'Line Code
' => '',
),
2 =>
array (
'Ordernumber' => '100000003',
'Orderdate' => '07/10/2013',
'OrderStatus' => 'pending',
'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
'PaymentMethod' => 'checkmo',
'ShippingMethod' => 'flatrate_flatrate',
'Subtotal' => '1716.5000',
'ShippingCost' => '5.0000',
'GrandTotal' => '1721.5000',
'TotalTax' => '0.0000',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => 'Cardone High Pressure Diesel Injection Oil Pump',
'ItemSKU' => '2P-225 Cardone',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '1716.5019',
'CostPrice' => '',
'ItemOrdered' => '1.0000',
'ItemInvoiced' => '0.0000',
'ItemSent' => '0.0000',
'CustomerID' => '1',
'BillingFirstName' => 'Brian',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => 'Florida',
'BillingCountry' => 'US',
'ShippingFirstName' => 'Brian',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => 'Florida',
'ShippingCountry' => 'US',
'Vendor SKU' => '2P-225',
'Line Code
' => 'A1',
),
3 =>
array (
'Ordernumber' => '',
'Orderdate' => '',
'OrderStatus' => '',
'PurchasedWebsite' => '',
'PaymentMethod' => '',
'ShippingMethod' => '',
'Subtotal' => '',
'ShippingCost' => '',
'GrandTotal' => '',
'TotalTax' => '',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => '',
'ItemSKU' => '',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '',
'CostPrice' => '',
'ItemOrdered' => '',
'ItemInvoiced' => '',
'ItemSent' => '',
'CustomerID' => '',
'BillingFirstName' => '',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => '',
'BillingCountry' => '',
'ShippingFirstName' => '',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => '',
'ShippingCountry' => '',
'Vendor SKU' => '',
'Line Code
' => '',
),
);
When I attempt to echo $results['Vendor SKU'], or var_dump it.. the result is NULL even though I see a value set above. I also notice there is a syntax error at the top ('$ = array').
That being said, I'm ultimately just trying to get the values from the CSV because some of these values will be used to send order information to a third party vendor.
Can anyone point me in the right direction of how to fix my output so that I'll be able to echo the values correctly?
Thanks!
use the built in php function to parse your csv file rather than re-invent the wheel.
str_getcsv
http://php.net/manual/en/function.str-getcsv.php
There's a good example on the man page that essentially does what you want:
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
print_r($data);
Simple, use the preset CSV reading method:
$fp = fopen('data.csv', 'r');
$rows = array();
while (($row = fgetcsv($fp)) !== false)
$rows[] = $row;
fclose($fp);
$rows will then contain all data of data.csv as a two-dimensional array.
See fgetcsv() for more info.

How to check double fields in multidimensional array?

How to check double fields in multidimensional array?
My array:
array (
'welcome' =>
array (
0 =>
array (
'url' => '/strona-powitalna',
'component' => 'welcome',
'action' => 'getForumPosts',
'parameter' =>
array (
0 =>
array (
'name' => '',
'type' => '',
'default' => '',
'method' => '',
),
),
'parameters_all' => '1',
'parameters_get' => 0,
'parameters_post' => 0,
),
),
'logi' =>
array (
),
'main' =>
array (
0 =>
array (
'url' => '/refresh',
'component' => 'main',
'action' => 'refreshPlayer',
'contype' => 'AJAX',
'parameter' =>
array (
0 =>
array (
'name' => '',
'type' => '',
'default' => '',
'method' => '',
),
),
'parameters_all' => '1',
'parameters_get' => 0,
'parameters_post' => 0,
),
),
'account' =>
array (
0 =>
array (
'url' => '/zmien-kolor',
'component' => 'account',
'action' => 'changeColorName',
'contype' => 'COMMON',
'parameter' =>
array (
0 =>
array (
'name' => 'rgb',
'type' => 'string',
'default' => '',
'method' => 'POST',
),
),
'parameters_all' => '1',
'parameters_get' => 0,
'parameters_post' => 1,
),
1 =>
array (
'url' => '/kolor-nicku',
'component' => 'account',
'action' => 'colorName',
'contype' => 'COMMON',
'parameter' =>
array (
0 =>
array (
'name' => '',
'type' => '',
'default' => '',
'method' => '',
),
),
'parameters_all' => '1',
'parameters_get' => 0,
'parameters_post' => 0,
)));
I must check if parameter url is unique.
foreach($arrays as $array)
if(is_array($array))
foreach($array as $key => $data)
if(strcmp($key,'url') == 0 && strcmp($key,$url) == 0){
echo $url.' is not unique url!';
break;
}
where $url variable is the url that you are looking for in arrays.

PHP Multi-Dimensional Arrays Issue

Ok I have a mult-dimensional array which has the following structure...
0 =>
array (
'membership' =>
array (
'member' =>
array (
'name' => '',
'landline' => '',
'libcard' => '',
'mobile' => '',
'email' => '',
),
'updated_at' => '',
'member_id' => 12345,
'starts_at' => '',
'id' => 14,
'group_id' => 280,
'optional_field_values' =>
array (
0 =>
array (
'optional_field' =>
array (
'name' => '',
'updated_at' => '',
'id' => 1,
'group_id' => 280,
'description' => '',
'created_at' => '',
),
'updated_at' => '',
'optional_field_id' => 1,
'membership_id' => 14,
'id' => 4,
'value' => '12539267',
'created_at' => '',
),
),
'ends_at' => '',
'joining_fee' => 0,
'created_at' => '',
),
),
Now I can access everything inside Membership and inside Member using code like...
$member[0]['membership']['member']['name']
or
$member[0]['membership']['joining_fee']
But when ever I try to access stuff inside optional_field_values I get nothing returned...
Any ideas why this is not working?
Edit:
Trying to access the field using code like...
$member[0]['membership']['optional_field_values']['value']
$member[0]['membership']['optional_field_values'][0]['value']
^ Should work...
(Edited to match OP's edit)
How about :
$member[0]['membership']['optional_field_values'][0]['value']
You can iterate over all optional field values like this :
foreach ($member[0]['membership']['optional_field_values'] as $field)
echo $field['value'];

Categories