Getting a value of a PHP array - php

I have this array which I would like to get the individual values of each item:
This is the result of print_r($theme_option):
Array ( [side_bars] => Array ( [0] => Array ( [title] => Left Page Sidebar [sort] => 0 ) [1] => Array ( [title] => Right Page Sidebar [sort] => ) [2] => Array ( [title] => Left Blog Sidebar [sort] => ) [3] => Array ( [title] => Right Blog Sidebar [sort] => ) )
I can get at a single sidebar name by using:
$theme_option['side_bars'][0]['title'];
I tried to loop through and get the individual sidebars using this code but it doesn't return as expected.
global $theme_option;
if(isset($theme_option['side_bars']) && is_array($theme_option['side_bars'])){
foreach ($theme_option['side_bars'] as $key => $value) {
$theme_side_bars["$key"] = "$value";
}
}
What I ant to try and do is get the value of each "Sidebar" and place the "title" into a dropdown box.
Thanks

There is an array side_bars which contains an array with all the special infos.
With this code you should have an array with all titles stored in the variable $theme_side_bars.
global $theme_option;
$theme_side_bars = array();
if(isset($theme_option['side_bars']) && is_array($theme_option['side_bars'])){
foreach ($theme_option['side_bars'] as $index=>$arr) {
$theme_side_bars[] = $arr['title'];
}
}

Related

array index changing into string [name] not number

I have form (display.php) that will get multiple selected option from user. Then this selected option will be formatted to another page (page.php). The problem occur when I try to display those multiple selected option. The array index are changing into string [name]!
Array ( [0] => 3204120006 [1] => 3204120011 [2] => 3204120010 [3] => 3204120009 )
Array ( [name] => BIRU ) Array ( [name] => BOJONG ) Array ( [name] => MAJAKERTA ) Array ( [name] => MAJALAYA )
Here the code of above display.
<?php
if (isset($_POST["desas"])) {
$ddes=$_POST["desas"];
print_r ($ddes);
foreach ( $ddes as $iddesa ) {
$namadesa=mysql_query("SELECT name FROM villages WHERE id='$iddesa' ");
if ($namadesa) {
$datadesa = mysql_fetch_assoc($namadesa);
print_r($datadesa);
}
} else
$datadesa="";
}
?>
My question is how to change ([name]=>BIRU),([name]=>BOJONG) into index ([0]=>BIRU),([1]=>BOJONG) etc on those array? or something missing in mysql fetch?
you can use array_values. reference: https://secure.php.net/manual/en/function.array-values.php
$datadesa = array_values(array_values);

Creating directories based on array values

I get a response from an API call that looks like the following
Array
(
[_id] => aasdasdasdasdasd
[created] => 2017-01-16T14:11:54.616Z
[options] => Array
(
[title] => 1
[data] => Array
(
[0] => Array
(
[labelName] => Date
[labelValues] => Array
(
[0] => March 2016
)
)
[1] => Array
(
[labelName] => Title
[labelValues] => Array
(
[0] => Food
)
)
[2] => Array
(
[labelName] => Product
[labelValues] => Array
(
[0] => Rice
)
)
)
)
)
I then process the response by doing the following
$results = array();
foreach ($output['options']['data'] as $data) {
if (isset($data['labelValues'][0])) {
$results[$data['labelName']] = $data['labelValues'][0];
}
}
This leaves me with something along the lines of this
Array
(
[Date] => March 2016
[Title] => Food
[Product] => Rice
)
Putting it into an array was not my intention, it was mainly done to help me better understand the looping required to process the original data.
My main intention is to create directories from these values. The main directory will be the Date, within this should be the Title and within this should be Product. So for the above, the directory structure should be something like 2016 > Food > Rice.
In order to achieve this, I have come up with the following
foreach ($output['options']['data'] as $data) {
if (isset($data['labelValues'][0])) {
if($data['labelName'] == 'Date') {
if (preg_match('/\b\d{4}\b/', $data['labelValues'][0], $matches)) {
$results[$data['labelName']] = $matches[0];
if (!file_exists($matches[0])) {
mkdir($matches[0], 0777, true);
}
}
}
}
}
The above works well and creates the date folder for me. Where I am struggling is how I now create the Title folder within the Date folder, and then the Product within the Title.
How would I go about achieving this?
Many thanks
I like your created array:
$array = array
(
[Date] => March 2016
[Title] => Food
[Product] => Rice
)
Simply implode the path:
mkdir(implode('/', $array), 0777, true);
This will create all of the directories, March 2016/Food/Rice

Reading Array and Getting Variable Output

By default, Gravity Form merge tags for checkboxes output the value of the selected option. If they choices for a question are English, Spanish, and Other, and the user selects English, the merge tag outputs:
english
My end goal is to be able to customize a Gravity Form merge tag output for checkboxes to include all of the options, checkbox-like format, and the selected option (checkbox is checked).
Right now, I'm trying to at least be able to return all of the options. Using this function,
`
add_action( 'gform_pre_submission_3', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
foreach($form['fields'] as &$field) {
if($field['id'] === 13) {
//$selectData['inputs'] = $field['inputs'];
$selectData['selections'] = $field['choices'];
$the_fields = $selectData;
foreach ($the_fields as $fields) {
print_r($fields);
}
}
}
I can get an output:
Array (
[0] => Array (
[text] => English
[value] => Language: English
[isSelected] =>
[price] =>
)
[1] => Array (
[text] => Spanish
[value] => Language: Spanish
[isSelected] =>
[price] =>
)
[2] => Array (
[text] => Other (specify)
[value] => Other
[isSelected] =>
[price] =>
)
)
I think that I'm going to need to get the [text] for each of the sub arrays into a new array. What's the syntax like to do that?
With the new array, I will be able to do a foreach loop, wrapping each element in HTML, using CSS to style the output like a checkbox.
As shorty:
$new_array = array_map(function($a){return $a['text'];},$fields);
;)

Unable to access nested array in php

I have an array like them i would like to access this sub category array like this
foreach($data as $parent_category){
$ndata=$parent_category['subCategory'];
foreach ($ndata as $subCategory){
}
}
Where $data is my main array print_r($data) give this output
When i access this array i got an error Undefined index: subCategory
Help me please ...
Array (
[1] => Array
(
[name] => Indian Culture
[subCategory] => Array
(
[0] => Array
(
[name] => Indain Culture-1
[articleId] => 10
)
[1] => Array
(
[name] => culture -1
[articleId] => 22
)
)
)
[5] => Array
(
[name] => ABC CULTURE
)
)
As You see, here:
[5] => Array
(
[name] => ABC CULTURE
)
Your array does not contain element with index "subCategory". So just check, that the index is present by invoking:
...
if (isset($parent_category['subCategory'])) {
...
Array key 5 doesn't have a subCategory index.
The first entry in the array will be fine.
You can check if subCategory is present using the array_key_exists function.
http://uk1.php.net/array_key_exists
Difference between isset and array_key_exists
Item with key 5 does not contain subCategory key. To avoid warnings, try with:
foreach($data as $parent_category){
if (isset($parent_category['subCategory'])) {
$ndata = $parent_category['subCategory'];
foreach ($ndata as $subCategory){
}
}
}
Modified your code as below, so you will not get undefined index for those cases where you didn't have subcategory.
foreach($data as $parent_category){
$ndata=isset($parent_category['subCategory'])?$parent_category['subCategory']:'';
if(!empty($ndata)){
foreach ($ndata as $subCategory){
}
}
}

Getting array value

My debugging output below
Array (
[0] => Array (
[re_text_field_id] => No posts to display
[re_textarea_field_id] => boring jokes
[re_image_field_id] => Array (
[id] => 6
[src] => http://localhost/project/wp-content/uploads/2014/02/bg1.gif
)
) [1] => Array (
[re_text_field_id] => fuck it
[re_textarea_field_id] => I feel dumb for asking, but how on earth would i go about using the repeatable fields in a way which outputs the fields needed
[re_image_field_id] => Array (
[id] => 4 [src] => http://localhost/project/wp-content/uploads/2014/02/bg-gradient1.png
)
)
)
To get text field I used
$options = get_option('demo_options')
$slides = $options['re_'];
foreach ($slides as $slide) {
echo $slide['re_textarea_field_id'];
}
But I am unable to get image path somewhat like this way
echo $slide['re_image _field_id'];
Try this, to retrive the path of the image. Need to add ['src']
echo $slide['re_image_field_id']['src'];

Categories