0

How can i get those variables and store the in my own variables? I want to get them in order to update some of them during the save process. I know that can happen via jquery/ajax or php.. but how I can do that correctly? I want for example to take the [x] and store it in my variable A.

 Array
 (
 [sliders] => Array
 (
 [c1] => Array
 (
 [content] => Array
 (
 [0] => Array
 (
//I WANT THAT VARIABLES: FROM title to additional style//
 [title] => 1
 [content_type] => image_content
 [content_url] => http://url.com
 [original_x] => -4px
 [original_y] => -27px
 [x] => -71.5
 [y] => -59
 [w] => 450
 [h] => 265
 [w_original] => 362
 [h_original] => 213
 [angle] => 0
 [opacity] => 100
 [order_position] => 10001
 [link_url] => 
 [matrix] => matrix(0.701111, 0, 0, 0.758491, 0, 0)
 [scaleX] => 0.70111111111111
 [scaleY] => 0.75849056603774
 [text_style] => 
 [additional_style] => Array
 (
 )
 [animation_fx] => Array
 (
 [fx_in] => 
 [fx_out] => 
 )
 [animation_times] => Array
 (
 [in_start] => 0
 [in_stop] => 1000
 [out_start] => 4000
 [out_stop] => 5000
 )
 )
 )
 [duration] => 5
 [overlap] => 0
 [order_position] => 1
 [min_z] => 10001
 [max_z] => 10001
 )
 )
 [width] => 450
 [height] => 300
 [back_color] => #ffffff
 [back_color_xyz] => Array
 (
 [x] => 8
 [y] => 50
 [z] => 0
 )
 [link_url] => 
 [title] => 
 [thumbnail] => 
 [id] => 0
 )

how can I get them via php? They are stored in the DB.

Mykola
3,4136 gold badges27 silver badges40 bronze badges
asked Jan 11, 2016 at 12:49
2
  • How did you print above data ? Commented Jan 11, 2016 at 13:02
  • Possible duplicate of PHP - get data from db to JSON Commented Jan 11, 2016 at 13:10

2 Answers 2

0

You can use variable variables to get the values, from title to additional_style array and store them in variables which are same as the array keys.

Here's the reference:

So your code should be like this:

// suppose $arr is your original array
foreach($arr['sliders']['c1']['content'][0] as $key => $value){
 $$key = $value; // variable variables
 if($key == "additional_style"){
 break;
 }
}
echo "title: " . $title . "<br />"; // title
echo "content_type: " . $content_type . "<br />"; // content_type
echo "content_url: " . $content_url . "<br />"; // content_url
echo "original_x: " . $original_x . "<br />"; // original_x
echo "original_y: " . $original_y . "<br />"; // original_y
echo "x: " . $x . "<br />"; // x
echo "y: " . $y . "<br />"; // y
echo "w: " . $w . "<br />"; // w
echo "h: " . $h . "<br />"; // h
echo "w_original: " . $w_original . "<br />"; // w_original
echo "h_original: " . $h_original . "<br />"; // h_original
echo "angle: " . $angle . "<br />"; // angle
echo "opacity: " . $opacity . "<br />"; // opacity
echo "order_position: " . $order_position . "<br />"; // order_position
echo "link_url: " . $link_url . "<br />"; // link_url
echo "matrix: " . $matrix . "<br />"; // matrix
echo "scaleX: " . $scaleX . "<br />"; // scaleX
echo "scaleY: " . $scaleY . "<br />"; // scaleY
echo "text_style: " . $text_style . "<br />"; // text_style
echo "additional_style array: "; var_dump($additional_style); // additional_style array

Output:

title: 1
content_type: image_content
content_url: http://url.com
original_x: -4px
original_y: -27px
x: -71.5
y: -59
w: 450
h: 265
w_original: 362
h_original: 213
angle: 0
opacity: 100
order_position: 10001
link_url:
matrix: matrix(0.701111, 0, 0, 0.758491, 0, 0)
scaleX: 0.70111111111111
scaleY: 0.75849056603774
text_style:
additional_style array:
array (size=0)
 empty 
answered Jan 11, 2016 at 13:21
Sign up to request clarification or add additional context in comments.

Comments

0

Two additions to Rajdeep Paul's answer:

  1. If your input is a JSON string, and not a PHP array, you could decode it giving a PHP object. Then you would refer the "content" objects like this:

    $obj->sliders->c1->content[0]

    instead of

    $arr['sliders']['c1']['content'][0]

  2. Assigning an array to a variable variable: One can not directly assign values to a variable variable. But, if a variable is an array, you could assign its item values to a temporary array and then assign this temporary array to the variable variable.

Example that shows this - with a PHP object decoded from a JSON string:

$php_object = json_decode($json);
foreach($php_object->sliders->c1->content[0] as $key => $value){
 if (is_array($value)) {
 $tmp_array = array();
 foreach($value as $akey => $avalue) {
 $tmp_array[] = $avalue;
 }
 $$key = $tmp_array;
 } else {
 $$key = $value; // variable variables
 }
}
echo "x: " . $x . "<br />"; // x
echo "y: " . $y . "<br />"; // y
echo 'additional_style:<br />'; // additional_style
foreach($additional_style as $style) echo '&nbsp;&nbsp;&nbsp;' . $style . "<br />";

Output:

x: -71.5
y: -59
additional_style:
 style_def_1
 style_def_2
 style_def_3
answered Jan 11, 2016 at 14:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.