Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php convert print_r to array

<?php
    //The array we begin with
    $start_array = array('foo' => 'bar', 'bar' => 'foo', 'foobar' => 'barfoo');

    //Convert the array to a string
    $array_string = print_r($start_array, true);

    //Get the new array
    $end_array = text_to_array($array_string);

    //Output the array!
    print_r($end_array);

    function text_to_array($str) {
        $keys = array();
        $values = array();
        $output = array();

      	if( substr($str, 0, 5) == 'Array' ) {
            $array_contents = substr($str, 7, -2);
            $array_contents = str_replace(array('[', ']', '=>'), array('#!#', '#?#', ''), $array_contents);
            $array_fields = explode("#!#", $array_contents);

            for($i = 0; $i < count($array_fields); $i++ ) {
                if( $i != 0 ) {
                    $bits = explode('#?#', $array_fields[$i]);
                    if( $bits[0] != '' ) $output[$bits[0]] = $bits[1];
                }
            }
            return $output;
        } else {
            echo 'The given parameter is not an array.';
            return null;
        }

    }
?>
Comment

PREVIOUS NEXT
Code Example
Php :: php strict mode 
Php :: drop column migration laravel 
Php :: increase php memory 
Php :: name of today php 
Php :: send email in php 
Php :: string match in php 
Php :: laravel eloquent group by week 
Php :: and php 
Php :: laravel get mysql column datatype 
Php :: laravel cache put array 
Php :: display image in html using php 
Php :: create controller with model resources and request command in laravel 
Php :: reCAPTCHA v3 PHP 
Php :: transient wordpress 
Php :: cronjob php linux 
Php :: submonth carbon 
Php :: how to log object laravel logger 
Php :: laravel return response view 
Php :: get key of array element php 
Php :: wordpress create shortcode 
Php :: php remove first word from string 
Php :: Laravel Model Create Artisan Commant 
Php :: ci base url dynamic 
Php :: php if elseif 
Php :: url() inside laravel config files 
Php :: wordpress admin url 
Php :: CSV File Read using PHP fgetcsv() 
Php :: how naming resource routes laravel 
Php :: specification migration laravel 
Php :: get node id in twig drupal 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =